Roman to Integer——Math

Given a roman numeral, convert it to an integer.

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

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        numList = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}

        L = len(s)
        re = numList[s[-1]]
        for i in range(L-2, -1, -1):
        	if numList[s[i]] < numList[s[i+1]]:
        		re -= numList[s[i]]
        	else:
        		re += numList[s[i]]

        return re

 

猜你喜欢

转载自qu66q.iteye.com/blog/2317222