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
| class Solution { public int[] decode(int[] encoded, int first) { int n = encoded.length; int[] arr = new int[n+1]; arr[0] = first; for(int i = 0; i < n; i++){ int k = encoded[i]; int j = arr[i]; int ans = 0; int cnt = 0; while(k!=0){ int x; int a = (k & 1); int b = (j & 1); if(a == 1){ if(b == 0){ x = 1; }else{ x = 0; } }else{ if(b == 0){ x = 0; }else{ x = 1; } } ans = ans | x << cnt; cnt++; k = k >> 1; j = j >> 1; } if(j != 0){ ans = ans | (j << cnt); } arr[i+1] = ans; } return arr; } }
|