Easy to understand python solution beats 100%


  • 0
    S

    Use a simple hashtable.

        def romanToInt(self, s):
            """
            :type s: str
            :rtype: int
            """
            table = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
            r = 0
            for i in range(len(s)-1):
                val = table[s[i]]
                if val >= table[s[i+1]]:
                    r += val
                else:
                    r -= val
            r += table[s[len(s)-1]]
            return r
    

Log in to reply
 

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