Skip to content

Commit c1f7e13

Browse files
authored
Create Roman to Integer - Leetcode 13.py
1 parent a74f665 commit c1f7e13

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Roman to Integer - Leetcode 13.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
d = {'I': 1, 'V':5, 'X':10, 'L':50, 'C':100, 'D': 500, 'M':1000}
4+
summ = 0
5+
n = len(s)
6+
i = 0
7+
8+
while i < n:
9+
if i < n - 1 and d[s[i]] < d[s[i+1]]:
10+
summ += d[s[i+1]] - d[s[i]]
11+
i += 2
12+
else:
13+
summ += d[s[i]]
14+
i += 1
15+
16+
return summ
17+
# Time: O(n)
18+
# Space: O(1)

0 commit comments

Comments
 (0)