-
Notifications
You must be signed in to change notification settings - Fork 0
/
plusone.java
48 lines (46 loc) · 1.16 KB
/
plusone.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
public class Solution {
public ArrayList<Integer> plusOne(ArrayList<Integer> A) {
ArrayList<Integer> st = new ArrayList<Integer>();
int length=A.size()-1;
int last = A.get(length);
boolean carry = false;
boolean extra= false;
if(last ==9) carry=true;
while(carry){
st.add(0);
if(length<1){
extra= true;
break;
}
int element = A.get(length-1);
if(element==9){
carry=true;
}
else carry=false;
length--;
}
if(!extra){
st.add(A.get(length)+1);
length--;
for(int i=length;i>=0;i--){
st.add(A.get(i));
}
}
else{st.add(1);}
boolean zeroCheck = false;
int outLen =st.size()-1;
if(st.get(outLen)==0){zeroCheck = true;}
while (zeroCheck){
st.remove(outLen);
if(length<1){
break;
}
int element = st.get(outLen-1);
if(element ==0) zeroCheck = true;
else zeroCheck = false;
outLen--;
}
Collections.reverse(st);
return st;
}
}