-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode13 roman2integer.py
50 lines (45 loc) · 1.39 KB
/
leetcode13 roman2integer.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# encoding: utf-8
'''
@author: Lingcheng Dai
@contact: [email protected]
@file: leetcode13 roman2integer.py
@time: 2018/12/19 10:19
'''
ss = "III"
class Solution:
# def romanToInt(self, s):
# """
# :type s: str
# :rtype: int
# """
# #实际一个dict就ok
# romandict = {'I': 0, 'V': 1, 'X': 2, 'L': 3, 'C': 4, 'D': 5, 'M': 6}
# numdict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
# savelist = []
# sum = 0
#
# while s != "":
# if len(s) >= 2:
# if romandict[s[0]] >= romandict[s[1]]:
# savelist.append(s[0])
# s = s[1:]
# else:
# savelist.append(s[0:2])
# s = s[2:]
# else:
# savelist.append(s[0])
# s = ""
# for roman_c in savelist:
# if len(roman_c) == 1:
# sum = sum + int(numdict[roman_c])
# else:
# sum = sum + (int(numdict[roman_c[1]]) - int(numdict[roman_c[0]]))
# return sum
def romanToInt(self, s):
d = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
res, p = 0, 'I'
for c in s[::-1]:
res, p = res - d[c] if d[c] < d[p] else res + d[c], c
return res
s = Solution()
print(s.romanToInt(ss))