Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
分析:该题是加法进位问题。
class Solution66{
public:
vector<int> plusOne(vector<int> &digits) {
vector<int> res(digits.size(), 0);
int sum = 0;
int one = 1;
for (int i = digits.size() - 1; i >= 0; i--) {
sum = one + digits[i];
one = sum / 10;
res[i] = sum % 10;
}
if(one > 0) {
res.insert(res.begin(), one);
}
return res;
}
};
本文详细解析如何通过编程解决给定非负数数组表示的加法进位问题,包括算法实现与步骤详解。
1432

被折叠的 条评论
为什么被折叠?



