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,返回结果:9+1 = 10;
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> result;
int addbit = 0;
int one;
for(int i=digits.size()-1;i>=0;i--)
{
if(i==digits.size()-1)
one = 1;
else
one = 0;
if(digits[i]+one+addbit==10)
{
addbit = 1;
result.push_back(0);
}
else
{
result.push_back(digits[i]+addbit+one);
addbit = 0;
}
}
if(addbit == 1)
result.push_back(1);
for(int j=0; j<result.size()/2;j++)
{
int temp = result[j];
result[j] = result[result.size()-1-j];
result[result.size()-1-j] = temp;
}
return result;
}
};