Simple python solution


  • 0
    U
                'I':1,            
                'V':5,
                'X':10, 
                'L':50,
                'C':100,
                'D':500,
                'M':1000
            }
             
            s = s[::-1]
            num = 0
            for i in range(len(s)):
                if i == 0:
                    num = N[s[i]]
                elif  N[s[i]] <  N[s[i-1]]:
                    num = num -  N[s[i]]
                else:
                    num +=  N[s[i]]
            
    
            return num
    

Log in to reply
 

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