-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode.py
More file actions
54 lines (50 loc) · 1.34 KB
/
Copy pathleetCode.py
File metadata and controls
54 lines (50 loc) · 1.34 KB
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
class Solution:
def romanToInt(self, s: str):
count=0
i=0
while i <len(s):
if s[i] == 'I':
if s[i+1] == 'V':
count+=4
i+=2
elif s[i+1] == 'X':
count+=9
i+=2
else:
count+=1
i+=1
elif s[i] == 'X':
if s[i+1] == 'L':
count+=40
i+=2
elif s[i+1] == 'C':
count+=90
i+=2
else:
count+=10
i+=1
elif s[i] == 'C':
if s[i+1] == 'D':
count+=400
i+=2
elif s[i+1] == 'M':
count+=900
i+=2
else:
count+=100
i+=1
elif s[i] == 'V':
count+=5
i+=1
elif s[i] == 'L':
count+=50
i+=1
elif s[i] =='D':
count+=500
i+=1
else:
count+=1000
i+=1
return count
sol =Solution()
print(sol.romanToInt('III'))