-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13.py
30 lines (28 loc) · 940 Bytes
/
13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
# 倒序处理
def romanToInt1(self, s):
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
val = 0
prev_roman = 0
rev_s = s[::-1] # 倒序
for i in range(len(s)):
roman_to_int = roman_dict[rev_s[i]]
if roman_to_int < prev_roman:
val = val - roman_to_int
else:
val = val + roman_to_int
prev_roman = roman_to_int
return val
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
num = 0
dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000, 'IV': -2, 'IX': -2, 'XL': -20, 'XC': -20, 'CD': -200, 'CM': -200}
for key in dict:
num = num + s.count(key) * dict[key]
return num
if __name__ == "__main__":
s = "MCMXCIV"
print(Solution().romanToInt1(s))