-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path66.2.cpp
More file actions
executable file
·72 lines (61 loc) · 1.61 KB
/
66.2.cpp
File metadata and controls
executable file
·72 lines (61 loc) · 1.61 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int>& digits)
{
std::vector<int> result;
int flag,counter = 0;
if (digits.back() < 9)//if not 9
digits.back() += 1;//we can just add one
else//if it is 9
{
for (std::vector<int>::reverse_iterator it = digits.rbegin(); it != digits.rend(); ++it)//tranverse to find the next not 9 digit
{
//cout << "iterator = " << *it << endl;
if (*it == 9)//set consistance 9 to 0
{
//cout << "here" << endl;
*it = 0;
counter++;
flag = 0;
}
else//until we find the non-9 digit, plus 1
{
*it += 1;
flag = 1;
break;
}
}
}
//cout << "Flag = " << flag << endl;
if (!flag)//if there is not non 9 digit.
{
//cout << "here" << endl;
digits.clear();
digits.push_back(1);
for (int i = 0; i < counter; ++i)
digits.push_back(0);
}
for (std::vector<int>::iterator it = digits.begin(); it != digits.end(); ++it)
result.push_back(*it);
digits.clear();
return result;
}
};
int main()
{
vector<int> a = {9,9};
//cout << "Size = " << a.size() << endl;
Solution S;
vector<int>res;
res = S.plusOne(a);
//cout << "size " << res.size() << endl;
while (!res.empty())
{
cout << "Result = " << res.back() << endl;
res.pop_back();
}
return 0;
}