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.



Hide Tags

 

​Array​​​ ​​Math​


Hide Similar Problems

 

​(M) Multiply Strings​​​ ​​(E) Add Binary​


class Solution {
public:
//给你一个用数组表示的数,求加一之后的结果,结果还是用数组表示,因为只加1,所以各位都是9才考虑,多一位
vector<int> plusOne(vector<int>& digits) {

int len=digits.size();
int i;
for(i=len-1;i>=0;i--)
{
if(digits[i]!=9)
{
digits[i]+=1;
return digits;
}
else
{
digits[i]=0;
}
}
if(i<0)
{
digits.insert(digits.begin(),1);
}
return digits;
}
};