一、题目要求
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<int> plusOne(vector<int>& digits) {
stack<int> st1,st2;
//
if(digits.empty())
{
digits.push_back(1);
return digits;
}
//
if(digits.size()==1)
{
if(digits[0]+1>=10)
{
digits.pop_back();
digits.push_back(1);
digits.push_back(0);
}
else
digits[0]+=1;
return digits;
}
//
for(auto tmp=digits.begin();tmp!=digits.end();tmp++)
st1.push(*tmp);
int flag=1,val;
while(!st1.empty())
{
val=flag+st1.top();
st1.pop();
if(val>=10)
{
flag=1;
}
else
flag=0;
val%=10;
st2.push(val);
}
digits.clear();
if(flag)
digits.push_back(flag);
while(!st2.empty())
{
digits.push_back(st2.top());
st2.pop();
}
return digits;
}