Algorithm


Problem Name: 989. Add to Array-Form of Integer

The array-form of an integer num is an array representing its digits in left to right order.

  • For example, for num = 1321, the array form is [1,3,2,1].

Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.

 

Example 1:

Input: num = [1,2,0,0], k = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

Example 2:

Input: num = [2,7,4], k = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455

Example 3:

Input: num = [2,1,5], k = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021

 

Constraints:

  • 1 <= num.length <= 104
  • 0 <= num[i] <= 9
  • num does not contain any leading zeros except for the zero itself.
  • 1 <= k <= 104
 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public List addToArrayForm(int[] num, int k) {
    int carry = 0;
    int idx = num.length - 1;
    List < Integer> result = new ArrayList<>();
    while (idx >= 0 || carry > 0 || k > 0) {
      int temp = k % 10 + carry + (idx >= 0 ? num[idx--] : 0);
      result.add(temp % 10);
      carry = temp / 10;
      k /= 10;
    }
    Collections.reverse(result);
    return result;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = [1,2,0,0], k = 34

Output

x
+
cmd
[1,2,3,4]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const addToArrayForm = function(num, k) {
  const res = []
  for(let i = num.length - 1; i >= 0; i--) {
    const tmp = num[i] + k
    res.push(tmp % 10)
    k = ~~(tmp / 10)
  }

  while(k > 0) {
    res.push(k % 10)
    k  = ~~(k / 10)
  }
  res.reverse()
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = [1,2,0,0], k = 34

Output

x
+
cmd
[1,2,3,4]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def addToArrayForm(self, A, K):
        for i in range(len(A))[::-1]:
            A[i], K = (A[i] + K) % 10, (A[i] + K) // 10
        return [int(i) for i in str(K)] + A if K else A
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = [2,7,4], k = 181

Output

x
+
cmd
[4,5,5]

#4 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _0989_AddToArrayFormOfInteger
    {
        public IList < int> AddToArrayForm(int[] A, int K)
        {
            var result = new List<int>();
            var carry = 0;

            var i = A.Length - 1;
            while (i >= 0 && K > 0)
            {
                var sum = A[i--] + K % 10 + carry;
                K /= 10;

                var digit = sum % 10;
                carry = sum / 10;
                result.Add(digit);
            }

            while (i >= 0)
            {
                var sum = A[i--] + carry;
                var digit = sum % 10;
                carry = sum / 10;
                result.Add(digit);
            }

            while (K > 0)
            {
                var sum = K % 10 + carry;
                K /= 10;
                var digit = sum % 10;
                carry = sum / 10;
                result.Add(digit);
            }

            if (carry > 0)
                result.Add(carry);

            result.Reverse();
            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = [2,7,4], k = 181

Output

x
+
cmd
[4,5,5]
Advertisements

Demonstration


Previous
#988 Leetcode Smallest String Starting From Leaf Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#990 Leetcode Satisfiability of Equality Equations Solution in C, C++, Java, JavaScript, Python, C# Leetcode