一次遍历,只要满足左边小于右边,则减去;如若不然,则加上
class Solution:
def romanToInt(self, s: str) -> int:
hashtable={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
n=len(s)
re=0
if not s:
return 0
for i in range(n-1):
a=hashtable[s[i]]
b=hashtable[s[i+1]]
if a<b:
re-=a
else:
re+=a
return re+hashtable[s[n-1]]