-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman to Integer
More file actions
34 lines (33 loc) · 932 Bytes
/
Roman to Integer
File metadata and controls
34 lines (33 loc) · 932 Bytes
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
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
s = s + "a"
ans = 0
for i in range(len(s)):
if(s[i] == "M"):
ans += 1000
if(s[i] == "D"):
ans += 500
if(s[i] == "C"):
if((s[i+1] == "M")|(s[i+1] == "D")):
ans -= 100
else:
ans += 100
if(s[i] == "L"):
ans += 50
if(s[i] == "X"):
if((s[i+1] == "C")|(s[i+1] == "L")):
ans -= 10
else:
ans += 10
if(s[i] == "V"):
ans += 5
if(s[i] == "I"):
if((s[i+1] == "X")|(s[i+1] == "V")):
ans -= 1
else:
ans += 1
return ans