注意连续进位和首位进位。
时间复杂度:O(N)
C++代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
if (*(digits.end() - 1) != 9)
*(digits.end() - 1) += 1;
else
{
auto it = digits.end() - 1;
while (*it == 9)
{
if (it == digits.begin())
{
*it = 1;
digits.push_back(0);
return digits;
}
*it = 0;
it--;
}
*it+=1;
}
return digits;
}
};