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.
Subscribe to see which companies asked this question
解题思路: 只有两种情况,一种是不是全部是9的,一种是全部是9的。如果是全部是9的情况,就在前面插入一个1即可。
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = 0;
for(i= digits.size() -1 ; i >= 0 ; i--)
{
if(digits[i] < 9)
{
digits[i]++;
return digits;
}else
{
digits[i]=0;
}
}
if(i<0)
{
digits.insert(digits.begin(),1);
}
return digits;
}
};