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.
分析:先将结果逆序存在stack里,再存到返回结果里
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int n = digits.size();
vector<int> ret;
if(n == 0)return ret;
stack<int> a;
int b = 1;
for(int i = n-1; i > -1; i--)
{
int m = digits[i]+b;
if(m == 10)
{
a.push(0);
b = 1;
}
else
{
a.push(m);
b = 0;
}
}
if(b == 1)a.push(1);
int s = a.size();
for(int i = 0; i < s; i++)
{
ret.push_back(a.top());
a.pop();
}
return ret;
}
};