-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path66. Plus One.cpp
More file actions
42 lines (41 loc) · 865 Bytes
/
66. Plus One.cpp
File metadata and controls
42 lines (41 loc) · 865 Bytes
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
class Solution {
public:
vector<int> plusOne(vector<int>& digits)
{
reverse(digits.begin(),digits.end());
int carry = 0;
bool flag = false;
digits[0] += 1;
if(digits[0]>=10)
{
digits[0] %= 10;
carry = 1;
flag = true;
}
else
{
carry = 0;
}
for(int i = 1;i<digits.size();i++)
{
flag = false;
digits[i] += carry;
if(digits[i]>=10)
{
digits[i] %= 10;
carry = 1;
flag = true;
}
else
{
carry = 0;
}
}
if(flag)
{
digits.push_back(1);
}
reverse(digits.begin(),digits.end());
return digits;
}
};