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.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
int Carry = 1;
const int SIZE = digits.size();
int Tmp = 0;
// add one
for (int Index = SIZE - 1; Index >= 0; Index--)
{
Tmp = digits[Index] + Carry;
if (Tmp < 10)
{
Carry = 0;
digits[Index] = Tmp;
break;
}
Carry = 1;
digits[Index] = Tmp - 10;
}
vector<int> Result;
if (Carry == 1)
{
Result.push_back(1);
}
for (int Index = 0; Index < SIZE; Index++)
{
Result.push_back(digits[Index]);
}
return Result;
}
};

原文地址:http://blog.csdn.net/sheng_ai/article/details/44900289