LeetCode1.8

作者: supermanwasd | 来源:发表于2019-01-08 22:48 被阅读0次

Roman to Integer

Screen Shot 2019-01-08 at 10.35.13 PM.png Screen Shot 2019-01-08 at 10.35.19 PM.png

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

相关文章

网友评论

    本文标题:LeetCode1.8

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