Skip to content

Commit 70743c3

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (65.62%), Memory - 17.8 MB (26.42%)
1 parent a70e1b4 commit 70743c3

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

0013-roman-to-integer/solution.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Approach 2 - Left-to-Right Pass Improved
1+
# Approach 2: Left to Right Pass Improved
2+
3+
# Time: O(n) = O(1), n = length of input
4+
# Space: O(1)
25

36
values = {
47
"I": 1,
@@ -10,26 +13,26 @@
1013
"M": 1000,
1114
"IV": 4,
1215
"IX": 9,
13-
"XL": 40,
16+
"XL": 40,
1417
"XC": 90,
1518
"CD": 400,
16-
"CM": 900
19+
"CM": 900,
1720
}
1821

22+
1923
class Solution:
2024
def romanToInt(self, s: str) -> int:
2125
total = 0
2226
i = 0
23-
27+
2428
while i < len(s):
25-
26-
# subtractive case
29+
# Subtractive case
2730
if i < len(s) - 1 and s[i : i + 2] in values:
2831
total += values[s[i : i + 2]]
2932
i += 2
30-
3133
else:
3234
total += values[s[i]]
3335
i += 1
34-
36+
3537
return total
38+

0 commit comments

Comments
 (0)