blob: 40ce008c29ab805c498d93af9498db11225bd04f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include <vector>
using std::vector;
class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
bool carry = true;
const auto end = digits.rend();
for (auto iter = digits.rbegin(); carry && iter != end; ++iter)
{
auto &digit = *iter;
digit += 1;
if (digit == 10)
{
digit = 0;
carry = true;
}
else
{
carry = false;
}
}
if (carry)
{
digits.insert(digits.cbegin(), 1);
}
return digits;
}
};
|