A Python solution not a fast one but a sample one

  • 0
    class Solution:
        # @return an integer
        def romanToInt(self, s):
            result = 0
            roman_dic = {'I':1, 'IV':3, 'V':5, 'IX':8, 'X':10, 'XL':30, 'L':50, 'XC':80, 'C':100, 'CD':300, 'D':500, 'CM':800, 'M':1000}
            for i in range(len(s)):
                if i > 0 and roman_dic.get(s[i-1:i+1]): 
                    temp = roman_dic.get(s[i-1:i+1])
                    temp = roman_dic[s[i]]
                result = result + temp
            return result

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.