-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddOneToNumber.cpp
More file actions
43 lines (30 loc) · 889 Bytes
/
AddOneToNumber.cpp
File metadata and controls
43 lines (30 loc) · 889 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
/*
Given a non-negative number represented as an array of digits,
add 1 to the number ( increment the number represented by the digits ).
The digits are stored such that the most significant digit is at the head of the list.
Example:
If the vector has [1, 2, 3]
the returned vector should be [1, 2, 4]
as 123 + 1 = 124.
LINK: https://www.interviewbit.com/problems/add-one-to-number/
*/
vector<int> Solution::plusOne(vector<int> &A) {
int n = A.size(), carry = 1;
vector<int> res;
for(int i = n-1; i>=0;i--){
int tmp = A[i]+carry;
if(tmp>=10){
carry = 1;
res.push_back(tmp%10);
}else{
carry = 0;
res.push_back(tmp);
}
}
if(carry==1){
res.push_back(1);
}
while(res.back()==0) res.pop_back();
reverse(res.begin(),res.end());
return res;
}