989. Add to Array-Form of Integer

For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  For example, if X = 1231, then the array form is [1,2,3,1].

Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

大整数相加模拟题

从后往前一位一位加,多余的往前insert

class Solution(object):
    def addToArrayForm(self, A, K):
        """
        :type A: List[int]
        :type K: int
        :rtype: List[int]
        """
        n = len(A)
        ans = [item for item in A]
        for i in range(n - 1, -1, -1):
            if K == 0:
                break
            value = K % 10
            K /= 10
            num = A[i] + value
            ans[i] = num % 10
            K += (num // 10)
        while K > 0:
            value = K % 10
            K /= 10
            ans.insert(0, value)
        return ans        
            

 

posted @ 2020-07-13 23:22  whatyouthink  阅读(92)  评论(0编辑  收藏  举报