-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman to Integer.py
More file actions
37 lines (34 loc) · 962 Bytes
/
Copy pathRoman to Integer.py
File metadata and controls
37 lines (34 loc) · 962 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
35
36
37
__author__ = 'Martin'
class Solution(object):
def romanToInt1(self, s):
"""
:type s: str
:rtype: int
"""
i = [1, 5, 10, 50, 100, 500, 1000]
r = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
res = 0
size = len(s)
for j in range(size):
if j < size-1:
if r.index(s[j]) < r.index(s[j+1]):
res -= i[r.index(s[j])]
else:
res += i[r.index(s[j])]
else:
res += i[r.index(s[j])]
return res
def romanToInt(self, s):
numerals = { "M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1 }
sum=0
s=s[::-1]
last=None
for x in s:
if last and numerals[x]<last:
sum-=2*numerals[x]
sum+=numerals[x]
last=numerals[x]
return sum
str = "DCXXI"
s = Solution()
print(s.romanToInt(str))