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.
直接show code!
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int i;
for (i = digits.size() - 1; i >= 0; --i)
{
if (digits[i] != 9){
++digits[i];
return digits;
}
else {
digits[i] = 0;
}
}
//各位全是9
if (i < 0) {
digits.insert(digits.begin(), 1);
}
return digits;
}
};
本文介绍了一种在数字以数组形式存储的情况下,对该数字加一的算法实现。该算法通过遍历数组从最低位开始检查并修改数字,适用于每一位都是9的特殊情况。
1387

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



