66. Plus One

Given a non-empty array of digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

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

数组表示大整数,求大整数+1后的数组

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        n = len(digits)
        ans = [0] * n
        carry = 1
        for i in range(n - 1, -1, -1):
            num = digits[i] + carry
            ans[i] = num % 10
            carry = num // 10
        if carry:
            ans.insert(0, carry)
        return ans
原文地址:https://www.cnblogs.com/whatyouthink/p/13301780.html