-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAddOneToNumber.cpp
More file actions
94 lines (61 loc) · 1.72 KB
/
AddOneToNumber.cpp
File metadata and controls
94 lines (61 loc) · 1.72 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
https://www.interviewbit.com/problems/add-one-to-number/
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.
*/
// # 1st method (May contains leading 0's)
vector<int> Solution::plusOne(vector<int> &A)
{
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int n = A.size();
for(int i=n-1; i>=0; i--)
{
if(A[i] < 9)
{
A[i]++;
return A;
}
A[i] = 0;
}
vector<int> v(n+1);
v[0] = 1;
return v; // O(n), O(1)
}
// # 2nd method
vector<int> Solution::plusOne(vector<int> &A)
{
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int carry=1;
vector<int> v;
for(int i=A.size()-1;i>=0;--i)
{
int sum;
sum=A[i]+carry;
carry=sum/10;
v.push_back(sum%10);
}
v.push_back(carry);
int i=v.size()-1;
vector<int> res;
while(i>=0 && v[i]==0)
{
i--;
}
while(i>=0)
{
res.push_back(v[i]);
i--;
}
return res; // O(n), O(n)
}