def romanToInt(self, s):
"""
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999
:type s: str
:rtype: int
"""
Dict_C = {'':0,'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} #0 1 5 10 50 100 500 1000
a_pre=0
sum_roman=0
for x in s:
if a_pre >= Dict_C[x] :
sum_roman=sum_roman+a_pre
else:
sum_roman=sum_roman-a_pre
a_pre=Dict_C[x]
return sum_roman+a_pre
romanToInt
最新推荐文章于 2022-03-08 17:39:50 发布