13. 罗马数字转整数
class Solution:
def romanToInt(self, s: str) -> int:
MAPPING = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000,
'IV':4,
'IX':9,
'XL':40,
'XC':90,
'CD':400,
'CM':900,
}
SPECIAL = {
'I':['V','X'],
'X':['L','C'],
'C':['D','M']
}
amount = 0
jump = False
for i in range(len(s)):
if jump:
jump = False
continue
if s[i] in SPECIAL:
if i < len(s)-1:
if s[i+1] in SPECIAL[s[i]]:
amount += MAPPING[s[i]+s[i+1]]
jump=True
else:
amount += MAPPING[s[i]]
else:
amount += MAPPING[s[i]]
else:
amount += MAPPING[s[i]]
return amount