Python iterative solution - O(n) 54ms


  • 0
    G
    class Solution:
        # @param {integer[]} digits
        # @return {integer[]}
        def plusOne(self, digits):
            result, carry = [], 1
            for num in digits[::-1]:
                temp = num + carry
                carry = 1 if temp / 10 else 0
                result.append(temp % 10)
    
            if carry:
                result.append(carry)
    
            return result[::-1]

Log in to reply
 

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