Skip to content

Commit e022cc5

Browse files
committed
Added python solution for 13. Roman to Integer.(Tahanima#101)
1 parent 23adcb4 commit e022cc5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
# Integer values of the roman numbers
4+
values = { "I": 1 , "V": 5 , "X": 10 , "L": 50 , "C": 100 , "D": 500 , "M": 1000 }
5+
6+
num = 0
7+
8+
left = 0
9+
10+
size = len(s)
11+
12+
for i in range(size-1, -1, -1):
13+
current = values[s[i]]
14+
15+
if (current >= left):
16+
num += current
17+
else:
18+
num -= current
19+
left = current
20+
21+
return num

0 commit comments

Comments
 (0)