1031. Add to Array-Form of Integer
My accepted C++ solution to LeetCode problem 1031, Add to Array-Form of Integer, running in 4ms.
- Difficulty: Easy
- C++
- Runtime 4ms
- Memory 31.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 4ms, memory 31.6MB, accepted 2025-12-27.
class Solution {
public:
vector<int> addToArrayForm(vector<int>& num, int k) {
for (int i = num.size() - 1; i >= 0 && k > 0; i--) {
num[i] += k;
k = num[i] / 10;
num[i] %= 10;
}
while (k > 0) {
num.insert(num.begin(), k % 10);
k /= 10;
}
return num;
}
};