• + 0 comments

    it's almost the same as this one in leetcode: https://leetcode.com/problems/roman-to-integer/description/

    but not using regex, enjoy:

    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
            s = s[::-1]
            last = None
            for i in s:
                if last and d[i]< last:
                    sum -= d[i] * 2
                sum += d[i]
                last = d[i]
            return sum