首页 > 代码库 > 【LeetCode】13. Roman to Integer

【LeetCode】13. 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

 

【LeetCode】13. Roman to Integer