您的位置:首页 > 其它

46. leetCode 13: Roman to Integer【罗马数转化为数字】

2018-02-06 15:28 288 查看
【题目】:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.
【参考】:参考资料
【Python代码】:

class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman_map = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
length = len(s)
result = roman_map[s[0]]
i = 1

while i < length:
if roman_map[s[i - 1]] < roman_map[s[i]]:
result = result + roman_map[s[i]] - 2*roman_map[s[i-1]]
else:
result = result + roman_map[s[i]]
i += 1

return result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: