5 line Python . Easy to understand


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

Log in to reply
 

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