66. Plus One
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.
一个非负整数n按照从高到低的顺序将每一位放在数组中,然后n加1,返回加1后的数组。
注意进位的问题。
代码如下:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int size = digits.size();
int flag = ;
for(int i = size-; i >= ; i --)
{
digits[i] = digits[i] + flag;
flag = ;
if(digits[i] > )
{
digits[i] = ;
flag = ;
}
}
if(flag == )
{
digits.insert(digits.begin(),flag);
}
return digits;
}
};