forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_to_integer.py
More file actions
52 lines (51 loc) · 1.71 KB
/
Copy pathroman_to_integer.py
File metadata and controls
52 lines (51 loc) · 1.71 KB
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
51
52
# -*- coding: utf-8 -*-
class Solution:
# @param {string} s Roman representation
# @return {int} an integer
def romanToInt(self, s):
# Write your code here
'''
罗马数字的特点是个十百千位的代表数字不一样。
1 - 10:I, II, III, IV, V, VI, VII, VIII, IX, X
10 - 100:X, XX, XXX, XL, L, LX, LXX, LXXX,XC, C
...
'''
ret = 0
i = 0
while i < len(s):
ch = s[i]
if ch == 'M': # 千位,题目限制小于等于3999
mul = 1000
one, five, ten = 'M', '?', '?'
elif (ch == 'C') or (ch == 'D'): # 百位
mul = 100
one, five, ten = 'C', 'D', 'M'
elif (ch == 'X') or (ch == 'L'): # 十位
mul = 10
one, five, ten = 'X', 'L', 'C'
else: # 个位
mul = 1
one, five, ten = 'I', 'V', 'X'
if ch == one:
tmp = 1
j = i + 1
while (j < len(s)) and (s[j] == one): # 处理连续的1
tmp += 1
j += 1
if (j < len(s)) and (s[j] == five): # 4
ret += 4 * mul
j += 1
elif (j < len(s)) and (s[j] == ten): # 9
ret += 9 * mul
j += 1
else:
ret += tmp * mul # 123
elif ch == five:
tmp = 5
j = i + 1
while (j < len(s)) and (s[j] == one):
tmp += 1
j += 1
ret += tmp * mul # 5678
i = j
return ret