Question

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

Solution

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        digits[-1] += 1
        i = len(digits) - 1
        while digits[i] == 10 and i > 0:
            digits[i] = 0
            digits[i - 1] += 1
            i -= 1
        if digits[0] == 10:
            digits[0] = 0
            digits = [1] + digits
        return digits