Skip to content

Commit e1ff6b4

Browse files
committed
improve -Solved a new EASY challenge in Leetcode array/string.
1 parent 7d93716 commit e1ff6b4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
"""
3+
Runtime: 42ms | Beats: 80.13%
4+
5+
Got help from Jadi :)
6+
"""
7+
8+
def romanToInt(self, s: str) -> int:
9+
conversion = {
10+
"I": 1,
11+
"V": 5,
12+
"X": 10,
13+
"L": 50,
14+
"C": 100,
15+
"D": 500,
16+
"M": 1000,
17+
"IV": 4,
18+
"IX": 9,
19+
"XL": 40,
20+
"XC": 90,
21+
"CD": 400,
22+
"CM": 900
23+
}
24+
25+
result = 0
26+
counter = 0
27+
while (counter < len(s)):
28+
if s[counter:counter+2] in conversion:
29+
result += conversion[s[counter:counter+2]]
30+
counter += 2
31+
else:
32+
result += conversion[s[counter]]
33+
counter += 1
34+
35+
return result
36+
37+
38+
problem_link = "https://leetcode.com/problems/roman-to-integer/submissions/1389192837/?envType=study-plan-v2&envId=top-interview-150"

0 commit comments

Comments
 (0)