class Solution {
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) {//k不为0就把k往进压 因为可能k比数组要长
res.push_back(k % 10);
}
reverse(res.begin(), res.end());
return res;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/add-to-array-form-of-integer/solution/shu-zu-xing-shi-de-zheng-shu-jia-fa-by-l-jljp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
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;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/add-to-array-form-of-integer/solution/shu-zu-xing-shi-de-zheng-shu-jia-fa-by-l-jljp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。