13. 罗马数字转整数

class Solution:
    def romanToInt(self, s: str) -> int:
        num = 0
        change = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000,
                  "IV": 4, "IX": 9, "XL": 40, "XC": 90, "CD": 400, "CM": 900}
        i = 0
        while i < len(s):
            if i + 1 <= len(s) - 1:
                if s[i: i + 2] in change:
                    num += change[s[i: i+2]]
                    i += 2
                    continue
            num += change[s[i]]
            i += 1
        return num

你可能感兴趣的:(13. 罗马数字转整数)