思路很简单 如果 一个数 小于后一个数 那么取负
Runtime: 180 ms, faster than 50.44% of Python3 online submissions for Roman to Integer.
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
dic = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
sum = 0
for i in range(len(s)):
if (i < (len(s) -1)) and (dic[s[i]] < dic[s[i+1]]):
dici = - dic[s[i]]
else:
dici = dic[s[i]]
sum += dici
return sum