My python code


  • 0
    S
    def romanToInt(self, s):
        roman = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
        res = 0
    
        for i in range(len(s)-1):
            l = roman[s[i]]
            r = roman[s[i+1]]
            res += l if l >= r else -l
    
        res += roman[s[-1]]
    
        return res
    

    same as the most of python code here, but i think it's easy to understand.


Log in to reply
 

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