13,罗马数字转整数

class Solution:
    def romanToInt(self, s: str) -> int:
        roman_dict = {
            'M':1000, 
            'D':500,
            'C':100, 
            'L':50, 
            'X':10, 
            'V':5, 
            'I':1
        }
        count = 0
        for i in range(len(s)-1):
            if roman_dict[s[i]] < roman_dict[s[i+1]]:
                count -= roman_dict[s[i]]
            else:
                count += roman_dict[s[i]]
        count += roman_dict[s[-1]]
        return count

猜你喜欢

转载自blog.csdn.net/weixin_42758299/article/details/88551430