-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplus_one.cpp
More file actions
63 lines (57 loc) · 1.33 KB
/
plus_one.cpp
File metadata and controls
63 lines (57 loc) · 1.33 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
/*
* =====================================================================================
*
* Filename: plus_one.cpp
*
* Description: Plus One: Given a non-empty array of digits representing a
* non-negative integer, plus one to the integer.
*
* Version: 1.0
* Created: 09/14/18 02:05:04
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
class Solution
{
public:
std::vector<int> plusOne(std::vector<int>& digits)
{
bool extra = false;
for (int i = (digits.size() - 1); i >= 0; i--)
{
digits[i] += 1;
if (digits[i] < 10)
{
break;
}
digits[i] -= 10;
if (i == 0)
{
extra = true;
}
}
if (extra)
{
digits.insert(digits.begin(), 1);
}
return digits;
}
};
int main(int argc, char* argv[])
{
std::vector<int> digits = {1, 2, 3};
auto nums = Solution().plusOne(digits);
for (auto n: nums)
{
printf("%d ", n);
}
printf("\n");
}