`

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics