Created
April 21, 2021 03:40
-
-
Save rohanjai777/fcff6b5c59d8607f46ebb16d42dede8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] plusOne(int[] A) { | |
int carry = 0; | |
int val = A[A.length-1]+1; | |
A[A.length-1] = val%10; | |
carry = val/10; | |
ArrayList<Integer> al = new ArrayList<Integer>(); | |
boolean flag = true; | |
for(int i=A.length-2;i>=0;i--){ | |
val = A[i]+carry; | |
A[i] = val%10; | |
carry = val/10; | |
} | |
if(carry>0){ | |
al.add(carry); | |
} | |
for(int i=0;i<A.length;i++){ | |
if(A[i]==0 && flag && al.size()==0){ | |
}else{ | |
flag = false; | |
al.add(A[i]); | |
} | |
} | |
int rs[] = new int[al.size()]; | |
for(int i=0;i<al.size();i++){ | |
rs[i] = al.get(i); | |
} | |
return rs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment