Leetcode13. 罗马数字转整数

class Solution:
def romanToInt(self, s: str) -> int:
Roman= ['I','V','X','L','C','D','M']
Int = [1,5,10,50,100,500,1000]
s1= 0
i=0
while i<len(s):
if i==len(s)-1:
s1+= Int[Roman.index(s[i])]
break
elif s[i]!=s[i+1] and Roman.index(s[i])<Roman.index(s[i+1]):
s1+= (Int[Roman.index(s[i+1])]-Int[Roman.index(s[i])])
i+=2
else:
s1+=Int[Roman.index(s[i])]
i+=1
return s1

浙公网安备 33010602011771号