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

  

posted @ 2017-12-16 14:15  PirateLHX  阅读(148)  评论(0编辑  收藏  举报