python 算法:羅馬數字轉化

class Solution:
    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)-1):
            a=s[i]
            b=s[i+1]
            if d[a]<d[b]:
                sum-=d[a]
            else:
                sum+=d[a]
        sum+=d[s[-1]]
        return sum
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章