题目:
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.
解答:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
if (digits.size() == 0) {
vector<int> rtn;
return rtn;
}
int *pData = new int [digits.size() + 1];
int carry = 1;
for (int i = digits.size() - 1; i >= 0; --i) {
int tmp = digits[i] + carry;
pData[i + 1] = (tmp % 10);
carry = tmp / 10;
}
int ix = 1;
if (carry) {
pData[0] = carry;
ix = 0;
}
vector<int> rtn(pData + ix, pData + digits.size() + 1);
delete [] pData;
return rtn;
}
};