13. Roman to Integer

Posted PirateLHX

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了13. Roman to Integer相关的知识,希望对你有一定的参考价值。

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

  

以上是关于13. Roman to Integer的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 13. Roman to Integer

LeetCode之Easy篇 ——(13)Roman to Integer

「Leetcode」13. Roman to Integer(Java)

LeetCode 13. Roman to Integer

13. Roman to Integer

#Leetcode# 13. Roman to Integer