-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddOneToNumber.cpp
More file actions
80 lines (65 loc) · 2.26 KB
/
addOneToNumber.cpp
File metadata and controls
80 lines (65 loc) · 2.26 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
/*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.
NOTE: Certain things are intentionally left unclear in this question which you should practice asking the interviewer.
For example, for this problem, following are some good questions to ask :
Q : Can the input have 0’s before the most significant digit. Or in other words, is 0 1 2 3 a valid input?
A : For the purpose of this question, YES
Q : Can the output have 0’s before the most significant digit? Or in other words, is 0 1 2 4 a valid output?
A : For the purpose of this question, NO. Even if the input has zeroes before the most significant digit.*/
vector<int> reverse(vector<int> b) // reverseing a list
{
int l = b.size() - 1;
int i = 0;
while(i<l)
{
int temp = b[i];
b[i]= b[l];
b[l] = temp;
i++;
l--;
}
return b;
}
vector<int> Solution::plusOne(vector<int> &A) {
vector<int> b = A;
if(b[0] == 0 && b.size() > 2)//removing zero from lsb
{
while(b[0]==0)
{
b.erase(b.begin());
}
}
int last = b.size()-1; // last element of array
if(b[last]< 9)// case when msb is less than 9 so just add 1 no need to handle carry
{
b[last] = b[last] + 1;
return b;
}
else //case when msb is equal to 9 , need to handle carry
{
b = reverse(b);
int j = 0;
while(b[j]==9 && j<b.size())//while getting 9 just change 9 to 0
{
b[j] = 0;
j++;
}
if(j== b.size())//if value is 99,999,9999,99999----- ,make array zero add extra element 1
{
b.push_back(1);
b = reverse(b);
return b;
}
else
{
b[j] = b[j] + 1 ;
b = reverse(b);
return b;
}
}
}