Python solution beats 95%


  • 1
    A
    class Solution(object):
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
            # We know the numbers are non negative so empty can only take value 1
            if not digits:
                return [1]
            # Remove the last digit and add 1 to it
            t = digits.pop() + 1
            if t < 10:
                digits.append(t)
            else:
                # take the LSB out and call the function recursively
                t = t % 10
                digits = self.plusOne(digits)
                # Reattach the LSB
                digits.append(t)
            return digits
    

Log in to reply
 

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