题解 | #给数组加一#
给数组加一
https://www.nowcoder.com/practice/e20d6e18e75941b6a5b7b33ffa7b8d4d
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @return int整型vector
*/
vector<int> plusOne(vector<int>& nums) {
int add = 1;
vector<int> res;
int n = nums.size();
// 从低位开始累加
for (int i = n - 1; i >= 0; --i) {
int cur = nums[i] + add;
int x = cur % 10;
add = cur / 10;
res.emplace_back(x);
}
if (add) res.emplace_back(add);
reverse(res.begin(), res.end());
return res;
}
};

