I will encode given integer as following encode rule
0 to [1]
1 to [0 1]
-1 to [-1 0 1]
999 to [000000000100000000010000000001]
public class EncodeThis { public static void main(String[] args) { int n = 999; int[] result= encodeThis(n); for(int i=0; i<result.length; i++) System.out.print(result[i]); } static int[] encodeThis(int n){ List<Integer> list = new ArrayList<>(); if(n==0) list.add(1); if(n<0) { n = n * -1; list.add(-1); } String string = ""+n; for(int i=0; i<string.length(); i++) { int digit = Integer.parseInt(""+string.charAt(i)); for(int j=1; j<=digit; j++){ list.add(0); } list.add(1); } int[] resultArray = new int[list.size()]; int j=0; for(int i:list) resultArray[j++] = i; return resultArray; } }
There was an error while loading. Please reload this page.