利用等价换算法将罗马数转为十进制数
class Solution:
def romanToInt(self, s: str) -> int:
roman_map={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
before_val,count=roman_map[s[0]],0
for c in s:
val=roman_map[c]
if val<=before_val:
count+=val
else:
count=count-val+2*(val-before_val)
before_val=val
return count
解题思路