码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode Roman to Integer python

时间:2015-11-19 00:19:55      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        numerals = { "M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1 }
        
        sums=0
        s=s[::-1]
        last=None
        
        for x in s:
            if last and numerals[x] < last:
                sums-=2*numerals[x]
            sums+=numerals[x]
            last=numerals[x]
        return sums

@link http://www.cnblogs.com/zuoyuan/p/3779688.html

leetcode Roman to Integer python

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/4976166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!