We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a70e1b4 commit 70743c3Copy full SHA for 70743c3
0013-roman-to-integer/solution.py
@@ -1,4 +1,7 @@
1
-# Approach 2 - Left-to-Right Pass Improved
+# Approach 2: Left to Right Pass Improved
2
+
3
+# Time: O(n) = O(1), n = length of input
4
+# Space: O(1)
5
6
values = {
7
"I": 1,
@@ -10,26 +13,26 @@
10
13
"M": 1000,
11
14
"IV": 4,
12
15
"IX": 9,
- "XL": 40,
16
+ "XL": 40,
17
"XC": 90,
18
"CD": 400,
- "CM": 900
19
+ "CM": 900,
20
}
21
22
23
class Solution:
24
def romanToInt(self, s: str) -> int:
25
total = 0
26
i = 0
-
27
28
while i < len(s):
- # subtractive case
29
+ # Subtractive case
30
if i < len(s) - 1 and s[i : i + 2] in values:
31
total += values[s[i : i + 2]]
32
i += 2
33
else:
34
total += values[s[i]]
35
i += 1
36
37
return total
38
0 commit comments