您的位置:首页 > 其它

13. Roman to Integer

2016-02-20 22:12 666 查看
Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

相比Interger to Roman,本题明显要简单一些,按照规则直接翻译就好。

AC代码:

class Solution(object):
def romanToInt(self, s):
roman_to_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
i, num = 0, 0
while i < len(s) - 1:
if roman_to_int[s[i]] >= roman_to_int[s[i + 1]]:
num += roman_to_int[s[i]]
else:
num -= roman_to_int[s[i]]
i += 1
num += roman_to_int[s[i]]
return num
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: