13_Roman to Integer
Last updated
Last updated
def romanToInt(s):
"""
:type s: str
:rtype: int
"""
# define the integer output
out = 0
# define the roman rule
roman = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}
# main body
for i in range(len(s)-1) :
if roman[s[i]] < roman[s[i+1]] :
out -= roman[s[i]]
else :
out += roman[s[i]]
return out + roman[s[-1]]