-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlus1.java
More file actions
69 lines (61 loc) · 2.08 KB
/
Copy pathPlus1.java
File metadata and controls
69 lines (61 loc) · 2.08 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
/*
* 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.
*/
import java.util.*;
public class Plus1{
public static ArrayList<Integer> plusOne(ArrayList<Integer> a) {
if(a == null ||a.size() == 0)
return null;
int n = a.size();
int carry = 1;
Integer zero = new Integer(0);
int sum = 0;
for(int i = n-1; i >= 0; i--){
sum = a.get(i) + carry;
carry = sum / 10;
sum = sum % 10;
a.set(i, sum);
}
if(carry != 0){
a.add(0, carry);
}
//int next =
for(int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + " ");
System.out.println();
int k = 0;
while(a.get(k).equals(zero)&& k < a.size()){
a.remove(k);
//++;
}
return a;
}
public static void main(String[] args){
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(2);
a.add(5);
a.add(6);
a.add(8);
a.add(6);
a.add(1);
a.add(2);
a.add(4);
a.add(5);
a = plusOne(a);
for(int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + " ");
}
}