classSolution { public: vector<int> addToArrayForm(vector<int>& num, int k){ vector<int> res; int n = num.size(); for (int i = n - 1; i >= 0; --i) { int sum = num[i] + k % 10; k /= 10; if (sum >= 10) { k++; sum -= 10; } res.push_back(sum); } for (; k > 0; k /= 10) { res.push_back(k % 10); } reverse(res.begin(), res.end()); return res; } };
classSolution { public List<Integer> addToArrayForm(int[] num, int k) { List<Integer> res = newArrayList<Integer>(); intn= num.length; for (inti= n - 1; i >= 0; --i) { intsum= num[i] + k % 10; k /= 10; if (sum >= 10) { k++; sum -= 10; } res.add(sum); } for (; k > 0; k /= 10) { res.add(k % 10); } Collections.reverse(res); return res; } }
funcaddToArrayForm(num []int, k int) (ans []int) { for i := len(num) - 1; i >= 0; i-- { sum := num[i] + k%10 k /= 10 if sum >= 10 { k++ sum -= 10 } ans = append(ans, sum) } for ; k > 0; k /= 10 { ans = append(ans, k%10) } reverse(ans) return }
funcreverse(num []int) { for i, n := 0, len(num); i < n/2; i++ { num[i], num[n-1-i] = num[n-1-i], num[i] } }
int* addToArrayForm(int* num, int numSize, int k, int* returnSize) { int* res = malloc(sizeof(int) * fmax(10, numSize + 1)); *returnSize = 0; for (int i = numSize - 1; i >= 0; --i) { int sum = num[i] + k % 10; k /= 10; if (sum >= 10) { k++; sum -= 10; } res[(*returnSize)++] = sum; } for (; k > 0; k /= 10) { res[(*returnSize)++] = k % 10; } for (int i = 0; i < (*returnSize) / 2; i++) { int tmp = res[i]; res[i] = res[(*returnSize) - 1 - i]; res[(*returnSize) - 1 - i] = tmp; } return res; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var addToArrayForm = function(num, k) { const res = []; const n = num.length; for (let i = n - 1; i >= 0; --i) { let sum = num[i] + k % 10; k = Math.floor(k / 10); if (sum >= 10) { k++; sum -= 10; } res.push(sum); } for (; k > 0; k = Math.floor(k / 10)) { res.push(k % 10); } res.reverse(); return res; };
classSolution { public: vector<int> addToArrayForm(vector<int>& num, int k){ vector<int> res; int n = num.size(); for (int i = n - 1; i >= 0 || k > 0; --i, k /= 10) { if (i >= 0) { k += num[i]; } res.push_back(k % 10); } reverse(res.begin(), res.end()); return res; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public List<Integer> addToArrayForm(int[] num, int k) { List<Integer> res = newArrayList<Integer>(); intn= num.length; for (inti= n - 1; i >= 0 || k > 0; --i, k /= 10) { if (i >= 0) { k += num[i]; } res.add(k % 10); } Collections.reverse(res); return res; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcaddToArrayForm(num []int, k int) (ans []int) { for i := len(num) - 1; i >= 0 || k > 0; i-- { if i >= 0 { k += num[i] } ans = append(ans, k%10) k /= 10 } reverse(ans) return }
funcreverse(num []int) { for i, n := 0, len(num); i < n/2; i++ { num[i], num[n-1-i] = num[n-1-i], num[i] } }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int* addToArrayForm(int* num, int numSize, int k, int* returnSize) { int* res = malloc(sizeof(int) * fmax(10, numSize + 1)); *returnSize = 0; for (int i = numSize - 1; i >= 0 || k > 0; --i, k /= 10) { if (i >= 0) { k += num[i]; } res[(*returnSize)++] = k % 10; }
for (int i = 0; i < (*returnSize) / 2; i++) { int tmp = res[i]; res[i] = res[(*returnSize) - 1 - i]; res[(*returnSize) - 1 - i] = tmp; } return res; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var addToArrayForm = function(num, k) { const res = []; const n = num.length; for (let i = n - 1; i >= 0 || k > 0; --i, k = Math.floor(k / 10)) { if (i >= 0) { k += num[i]; } res.push(k % 10); } res.reverse(); return res; };