13.Roman to Integer

来源:互联网 发布:美丽港发型设计软件 编辑:程序博客网 时间:2024/05/01 17:52

给你一个罗马数字,把它转化成整数

给的数字的范围在1-3999

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        dict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
        num = 0
        for i in range(len(s[:-1])):
            if dict[s[i]] <dict[s[i+1]]:
                num -= dict[s[i]]
            else:
                num += dict[s[i]]
        return num + dict[s[-1]]

0 0
原创粉丝点击