Time: 80 ms (21.17%), Space: 13.4 MB (57.48%) - LeetHub

This commit is contained in:
Deven
2022-07-13 15:27:16 -04:00
parent 4767ed834f
commit 62860fe75d
@@ -0,0 +1,35 @@
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
rToInt = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
mixes = {
"IV": 4,
"IX": 9,
"XL": 40,
"XC": 90,
"CD": 400,
"CM": 900
}
num = 0
while len(s) > 0:
if s[:2] in mixes:
num += mixes[s[:2]]
s = s[2:]
else:
num += rToInt[s[0]]
s = s[1:]
return num