美文网首页
LeetCode Roman to Integer

LeetCode Roman to Integer

作者: 蒜苗爱妞妞 | 来源:发表于2017-12-17 21:45 被阅读0次
class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        tmp = dict[s[0]]
        for dx, val in enumerate(s[1:]):
            val_p = dict[s[dx]]
            val = dict[val]
            if val_p >= val:
                tmp = tmp + val
            if val_p < val and s[dx] in ['I', 'X', 'C']:
                tmp = tmp + val - val_p * 2
        return tmp

相关文章

网友评论

      本文标题:LeetCode Roman to Integer

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