-
Notifications
You must be signed in to change notification settings - Fork 0
/
13罗马数字转整数.py
26 lines (26 loc) · 920 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
class Solution:
def romanToInt(self, s: str) -> int:
# value = {'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
value1 = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
z = 0
# for i,j in value.items():
# if s:
# if i in s:
# z += s.count(i)*j
# s=s.replace(i,'')
# else:
# return z
# for i1,j1 in value1.items():
# if s:
# if i1 in s:
# z += s.count(i1)*j1
# s=s.replace(i1,'')
# else:
# return z
for i in range(len(s)):
if i<len(s)-1 and value1[s[i]]<value1[s[i+1]]:
z-=value1[s[i]]
else:
z+=value1[s[i]]
return z
print(Solution().romanToInt('IX'))