1 line Python solution


  • 0
    L

    Logic: count each Roman letters and the combination letters to determine add or subtract

    def romanToInt(self, s):

        return s.count('M')*1000 + s.count('D')*500 + (s.count('C')-2*s.count('CM')-2*s.count('CD'))*100 + s.count('L')*50 + (s.count('X')-2*s.count('XL')-2*s.count('XC'))*10 + s.count('V')*5 + s.count('I')-2*s.count('IV')-2*s.count('IX')

Log in to reply
 

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