/*
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>
using std::cout;
using std::endl;
using std::vector;
std::vector<int>::iterator;
std::vector<int>::reverse_iterator;
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
vector<int>::reverse_iterator p;
for (p = digits.rbegin(); p != digits.rend(); p++) {
if (*p + 1 < 10){
*p = *p + 1;
break;
}
else {
*p = 0;
}
}
if (p == digits.rend()) {
digits.insert(digits.begin(), 1);
}
return digits;
}
};
void main()
{
vector<int> digits;
/*digits.push_back(1);
digits.push_back(3);
digits.push_back(4);*/
digits.push_back(9);
digits.push_back(9);
Solution slt;
slt.plusOne(digits);
}
leetcode - plus one
最新推荐文章于 2019-04-06 22:17:35 发布