Add to Array-Form of Integer

https://leetcode.com/problems/add-to-array-form-of-integer

# Solution

There are solutions to append the carry to result and reverse before return but not really necessary.

# Schoolbook Addition

Add K to last element in A, do bitwise addition, then handle the carry.

Complexity:

  • time: O(max(n,logK))O(max(n,\log K))
  • space: O(1)O(1)

where n is length of A.







 



def addToArrayForm(self, A: List[int], K: int) -> List[int]:
    A[-1] += K
    for i in reversed(range(len(A))):
        carry, A[i] = divmod(A[i], 10)
        if i: A[i-1] += carry
    if carry:
        # OR list comprehension: A = [int(i) for i in str(carry)] + A
        A = list(map(int, str(carry))) + A
    return A
Last Updated: 7/19/2020, 3:45:14 PM