Leetcode 1031 Solution

This article provides solution to leetcode question 1031 (add-to-array-form-of-integer)

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

Solution

class Solution:
    def addToArrayForm(self, A: List[int], K: int) -> List[int]:
        carry = 0
        i = len(A) - 1

        while K or carry:
            if i < 0:
                A.insert(0, 0)
                i = 0

            val = K % 10

            carry, A[i] = divmod(A[i] + val + carry, 10)

            K //= 10
            i -= 1

        return A