#989
Easy Algorithms Add to array form of integer
Array Math
45.4% acceptance
Feb 25, 2026
3679
316
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn add_to_array_form(num: Vec<i32>, k: i32) -> Vec<i32> {
let mut carry = k;
let mut res: Vec<i32> = Vec::new();
let mut i = num.len() as i32 - 1;
while i >= 0 || carry > 0 {
if i >= 0 { carry += num[i as usize]; i -= 1; }
res.push(carry % 10);
carry /= 10;
}
res.reverse();
res
}
}