Skip to content

Commit 43afb18

Browse files
committed
Add code for leetcode No.13.
1 parent 01cb3d8 commit 43afb18

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: leetcode-013/leetcode013.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
d = dict()
4+
d = {
5+
'I': 1,
6+
'V': 5,
7+
'X': 10,
8+
'L': 50,
9+
'C': 100,
10+
'D': 500,
11+
'M': 1000
12+
}
13+
14+
sum0 = 0
15+
for i in range(len(s)):
16+
currentValue = d[s[i]]
17+
18+
if i == len(s) -1 or d[s[i+1]] <= currentValue: # d[s[i+1]]: nextValue
19+
sum0 += d[s[i]]
20+
else:
21+
sum0 -= d[s[i]]
22+
23+
return sum0
24+
25+
sol = Solution()
26+
print(sol.romanToInt("MCMXCIV"))

0 commit comments

Comments
 (0)