-
Notifications
You must be signed in to change notification settings - Fork 31
/
AddOne.java
50 lines (45 loc) · 1.13 KB
/
AddOne.java
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
package Array;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 18/08/18
* Time - 3:16 PM
*/
public class AddOne {
public ArrayList<Integer> plusOne(ArrayList<Integer> A) {
int n = A.size();
int carry = (A.get(n-1)+1)/10;
A.set(n-1, (A.get(n-1) + 1)%10);
for (int i = n-2; i >= 0; i--) {
int temp = (A.get(i) + carry)/10;
A.set(i, (A.get(i) + carry)%10);
carry = temp;
if (carry == 0){
break;
}
}
if (carry == 1){
A.add(0, carry);
return A;
}
else{
int start = 0;
for (Integer aA : A) {
if (aA == 0) {
start++;
} else {
break;
}
}
return new ArrayList<>(A.subList(start, A.size()));
}
}
public static void main(String[] args) {
ArrayList<Integer> t = new ArrayList<>();
t.add(9);
t.add(9);
t.add(9);
t.add(9);
System.out.println(new AddOne().plusOne(t));
}
}