-
Notifications
You must be signed in to change notification settings - Fork 0
/
13. Roman to Integer.py
67 lines (63 loc) · 1.76 KB
/
13. Roman to Integer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution:
def romanToInt(self, s: str) -> int:
sum = 0
ctn = False
for i in range(len(s)):
if ctn:
ctn = False
continue
ch = s[i]
if i != len(s)-1:
next_ch = s[i+1]
else:
next_ch = None
if ch == 'I':
if next_ch == 'V':
sum += 4
ctn = True
continue
elif next_ch == 'X':
sum += 9
ctn = True
continue
else:
sum += 1
#i += 1
continue
elif ch == 'V':
sum += 5
continue
elif ch == 'X':
if next_ch == 'L':
sum += 40
ctn = True
continue
elif next_ch == 'C':
sum += 90
ctn = True
continue
else:
sum += 10
continue
elif ch == 'L':
sum += 50
continue
elif ch == 'C':
if next_ch == 'D':
sum += 400
ctn = True
continue
elif next_ch == 'M':
sum += 900
ctn = True
continue
else:
sum += 100
continue
elif ch == 'D':
sum += 500
continue
elif ch == 'M':
sum += 1000
continue
return sum