-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-RomanToInteger.py
42 lines (37 loc) · 1.16 KB
/
13-RomanToInteger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def romanToInt(self, s: str) -> int:
roman2int = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
i = 0
total = 0
while i < len(s):
ch = s[i]
if ch == "I":
if i + 1 < len(s) and s[i+1] in "VX":
total += roman2int[s[i+1]] - 1
i += 1
else:
total += 1
elif ch == "X" and i + 1 < len(s):
if i + 1 < len(s) and s[i+1] in "LC":
total += roman2int[s[i+1]] - 10
i += 1
else:
total += 10
elif ch == "C" and i + 1 < len(s):
if i + 1 < len(s) and s[i+1] in "DM":
total += roman2int[s[i+1]] - 100
i += 1
else:
total += 100
else:
total += roman2int[s[i]]
i += 1
return total