-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_romanToInt.py
53 lines (49 loc) · 1.35 KB
/
13_romanToInt.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
# 13. Roman to Integer
class Solution:
def romanToInt(self, s: str) -> int:
i=0
num = 0
while(i<len(s)):
if s[i]=='I':
if i<len(s)-1 and s[i+1]=='V':
num+=4
i+=2
elif i<len(s)-1 and s[i+1]=='X':
num+=9
i+=2
else:
num+=1
i+=1
elif s[i]=='V':
num+=5
i+=1
elif s[i]=='X':
if i<len(s)-1 and s[i+1]=='L':
num+=40
i+=2
elif i<len(s)-1 and s[i+1]=='C':
num+=90
i+=2
else:
num+=10
i+=1
elif s[i]=='L':
num+=50
i+=1
elif s[i]=='C':
if i<len(s)-1 and s[i+1]=='D':
num+=400
i+=2
elif i<len(s)-1 and s[i+1]=='M':
num+=900
i+=2
else:
num+=100
i+=1
elif s[i]=='D':
num+=500
i+=1
elif s[i]=='M':
num+=1000
i+=1
return num