-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_to_Integer.py
More file actions
51 lines (46 loc) · 1.32 KB
/
Copy pathRoman_to_Integer.py
File metadata and controls
51 lines (46 loc) · 1.32 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
class Solution(object):
def romanToInt(self, s):
integer = 0
roman_integer = {
"I" : 1,
"V" : 5,
"X" : 10,
"L" : 50,
"C" : 100,
"D" : 500,
"M" : 1000
}
i = 0
while i < len(s):
if i+1 < len(s):
if s[i] == 'I' and s[i+1] == 'V':
integer += 4
i += 2
continue
if s[i] == 'I' and s[i+1] == 'X':
integer += 9
i += 2
continue
if s[i] == 'X' and s[i+1] == 'L':
integer += 40
i += 2
continue
if s[i] == 'X' and s[i+1] == 'C':
integer += 90
i += 2
continue
if s[i] == 'C' and s[i+1] == 'D':
integer += 400
i += 2
continue
if s[i] == 'C' and s[i+1] == 'M':
integer += 900
i += 2
continue
integer += roman_integer[s[i]]
i +=1
return integer
"""
:type s: str
:rtype: int
"""