0%

989. Add to Array-Form of Integer

O(m+n) time O(1) space

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
int n = A.size();
int i = n - 1, c = 0;
vector<int> res;
while (i >= 0 || c > 0 || K > 0) {
int a = i >= 0 ? A[i] : 0;
int b = K % 10;
int s = a + b + c;
c = s / 10;
res.push_back(s % 10);
--i;
K /= 10;
}
return vector<int>(rbegin(res), rend(res));
}
};