美文网首页
LeetCode 13. Roman to Integer

LeetCode 13. Roman to Integer

作者: 费城的二鹏 | 来源:发表于2018-11-21 09:23 被阅读6次

Roman to Integer

class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """

        dic2 = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
        sum2 = 0
        lastNum = 0
        for char in s[::-1]:
            num = dic2[char]
            if lastNum > num:
                sum2 -= num
            else:
                sum2 += num
            lastNum = num

        print(sum2)
        return sum2

相关文章

网友评论

      本文标题:LeetCode 13. Roman to Integer

      本文链接:https://www.haomeiwen.com/subject/udkqqqtx.html