leetcode 013

Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Method

Pay attantion to a letter is in front or behind.

Python Code

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        numerals = { "M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1 }
        suum = 0
        prev = 10000
        for x in s:
            if numerals[x] > prev:
                suum = suum - 2 * prev + numerals[x]
            else:
                suum = suum + numerals[x]
            prev = numerals[x]        
        return suum