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.
注意点:1. insert的运用 2. 前缀为0的情况
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size() - 1;
for(; i>=0; i--)
{
if(digits[i]!=9)
{
digits[i]++;
break;
}
digits[i] = 0;
}
if(digits[0]==0 && i<0) //前缀为0用i<0判断
{
digits.insert(digits.begin(),1);
}
return digits;
}
};
本博客讨论了如何在给定非负整数作为数组形式表示的情况下,将其加一并返回更新后的数组。包括了对插入操作的应用及前缀为零情况的处理。
970

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



