Skip to content

Commit 55100e4

Browse files
add 1 code
1 parent e006f31 commit 55100e4

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-45
lines changed

.idea/workspace.xml

Lines changed: 50 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

leetcode/13. Roman to Integer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
Given a roman numeral, convert it to an integer.
4+
5+
Input is guaranteed to be within the range from 1 to 3999.
6+
7+
'''
8+
9+
class Solution(object):
10+
def romanToInt(self, s):
11+
romanDict = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
12+
res, p = 0, 'I'
13+
for ch in s[::-1]:
14+
if romanDict[ch] < romanDict[p]:
15+
res = res - romanDict[ch]
16+
else:
17+
res = res + romanDict[ch]
18+
p = ch
19+
return res
20+
21+
s = Solution()
22+
print((s.romanToInt("IX")))

0 commit comments

Comments
 (0)