Python solution without pow or multiplication.


  • 0
    W

    This is my Python solution without transforming the digits to integer, which requires extra expense on multiplications.

    class Solution(object):
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
            if digits[-1]!=9:
                digits[-1] += 1
            else:
                lt = len (digits)
                for i in xrange(lt-1, -1, -1):
                    if digits[i]==9:
                        digits[i]=0
                    else:
                        digits[i] += 1
                        break
                if digits[0]==0:
                    digits.insert (0,1)
            return digits
    

Log in to reply
 

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