class Solution:
def romanToInt(self, s: str) -> int:
tempA = {'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900,}
tempB = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500, 'M':1000,}
result = 0
if not s:
return result
while len(s) > 0:
if len(s) > 2:
if s[:2] in tempA.keys():
result += tempA[s[:2]]
s = s[2:]
continue
if len(s) == 2:
if s in tempA.keys():
result += tempA[s]
s = ''
continue
if s[0] in tempB.keys():
result += tempB[s[0]]
s = s[1:]
return result
s = Solution()
s.romanToInt('MCMXCIV')
github项目地址:https://github.com/JockWang/LeetCode-Python