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.
用vector表示一个数,对这个数进行加1操作。
AC code:
class Solution {
public:
vector<int> plusOne(vector<int> &digits)
{
int sum=1,n=digits.size();
for(int i=n-1;i>=0;i--)
{
digits[i]=(digits[i]+1);
if(digits[i]<10)
break;
else
{
digits[i]%=10;
if(i==0)
digits.insert(digits.begin(),1);
}
}
return digits;
}
};