Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size();
vector <int> ans;
int he, jingwei;
jingwei = 0;
if (len == 1)
{
he = digits[len - 1] + 1;
if (he == 10) jingwei = 1;
if (jingwei) { ans.push_back(1); ans.push_back(0); return ans; }
else { ans.push_back(he); return ans; }
}
he = digits[len - 1] + 1;
jingwei = he / 10;
he = he % 10;
ans.push_back(he);
for (int i = len - 2; i >=0; i--)
{
he = digits[i] + jingwei;
jingwei = he / 10;
he = he % 10;
ans.push_back(he);
}
if (jingwei) ans.push_back(1);
reverse(ans.begin(), ans.end());
return ans;
}
};