Add to Array-Form of Integer

problem at: 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:
  • space:

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:
        A = list(map(int, str(carry))) + A
        # above line could also use list comprehension:
        # A = [int(i) for i in str(carry)] + A
    return A