Add an integer to a number stored as an array of digits and return the result in the same format.
Add To Array Form Of Integer
You are given an integer represented as an array of digits num, where num[i] is the i-th digit of the number from most significant to least significant. You are also given a non-negative integer k.
Return the array-form of the sum num + k.
The result should also be returned as an array of digits in most-significant-first order.
This problem is about performing addition digit by digit, without converting the whole array-form number into a language-specific integer type.
Input Format
num: an array of decimal digits, most significant digit firstk: a non-negative integer
The task is to compute the decimal representation of num + k and return it as an array of digits.
Output Format
- Return an array of digits representing the sum in most-significant-first order.
Constraints
1 <= num.length- Each
num[i]is a digit from0to9 numdoes not contain leading zeros, except that the number0itself may appear as[0]0 <= k- The answer may be larger than standard fixed-width integer types, so carry-based addition is expected
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.
Show 1 more example
Example 3
Input
num = [9,9,9,9,9,9,9,9,9,9], k = 1
Output
[1,0,0,0,0,0,0,0,0,0,0]
Explanation
A final carry can create a new most significant digit.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.