-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplus-one.cpp
38 lines (30 loc) · 925 Bytes
/
plus-one.cpp
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
35
36
37
38
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int remaining = false;
bool plusOne = true;
int current = digits.size() - 1;
while (current >= 0 && (remaining || plusOne)) {
if (plusOne || remaining) {
digits[current] += 1;
if( digits[current] == 10) {
remaining = true;
digits[current] = 0;
} else{
remaining = false;
}
plusOne = false;
}
current--;
}
if (remaining) {
vector<int> result;
result.push_back(1);
for(int i: digits) {
result.push_back(i);
}
return result;
}
return digits;
}
};