LeetCode13. Roman to Integer

Posted wilderness

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode13. Roman to Integer相关的知识,希望对你有一定的参考价值。

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

题意:把罗马数字转变为数字

 1 class Solution(object):
 2     def romanToInt(self, s):
 3         """
 4         :type s: str
 5         :rtype: int
 6         """
 7         flag = 0
 8         sum = 0
 9         d = {M:1000, D:500, C:100, L:50, X:10, V:5, I:1}
10         for i in s[::-1]:
11             t = d[i]
12             if t>=flag:
13                 flag=t
14                 sum+=t
15             else:
16                 sum-=t
17                 
18         return sum

 

以上是关于LeetCode13. Roman to Integer的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 13. Roman to Integer

Leetcode 13. Roman to Integer(python)

Leetcode 13. Roman to Integer

Leetcode13 Roman To Integer

LeetCode13. Roman to Integer

LeetCode13. Roman to Integer