A simple iterative python solution


  • 0
    M
    class Solution(object):
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
            if (digits[-1]+1)<10:
                return digits[0:-1] + [digits[-1]+1]
            else:
                carry = 1
                i = len(digits)-1
                while(carry == 1):
                    
                    
                    temp = digits[i]
                    
                    digits[i] = (temp+1)%10
                    
                    carry = (temp+1)/10
                    
                    if i == 0 and carry==1:
                        return [1] + digits
                    
                    i = i-1
                    
                return digits    
    

Log in to reply
 

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