class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
tmp = dict[s[0]]
for dx, val in enumerate(s[1:]):
val_p = dict[s[dx]]
val = dict[val]
if val_p >= val:
tmp = tmp + val
if val_p < val and s[dx] in ['I', 'X', 'C']:
tmp = tmp + val - val_p * 2
return tmp
网友评论