首页 > 其他 > 详细

13. Roman to Integer

时间:2016-02-20 22:53:23      阅读:214      评论:0      收藏:0      [点我收藏+]

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

 

13. Roman to Integer

原文:http://www.cnblogs.com/zhuifengjingling/p/5204122.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!