File tree Expand file tree Collapse file tree 1 file changed +8
-12
lines changed
solution/1800-1899/1829.Maximum XOR for Each Query Expand file tree Collapse file tree 1 file changed +8
-12
lines changed Original file line number Diff line number Diff line change @@ -303,20 +303,16 @@ class Solution:
303
303
``` java
304
304
class Solution {
305
305
public int [] getMaximumXor (int [] nums , int maximumBit ) {
306
- int xs = 0 ;
307
- for (int x : nums) {
308
- xs ^ = x;
306
+ int xor = (int )(Math . pow(2 , maximumBit))- 1 ;
307
+ int [] answer = new int [nums. length];
308
+ for (int i = 1 ; i < nums. length; i++ ){
309
+ nums[i] ^ = nums[i- 1 ];
309
310
}
310
- int mask = (1 << maximumBit) - 1 ;
311
- int n = nums. length;
312
- int [] ans = new int [n];
313
- for (int i = 0 ; i < n; ++ i) {
314
- int x = nums[n - i - 1 ];
315
- int k = xs ^ mask;
316
- ans[i] = k;
317
- xs ^ = x;
311
+ int k= 0 ;
312
+ for (int i= nums. length- 1 ; i>= 0 ; i-- ){
313
+ answer[k++ ] ^ = nums[i]^ xor;
318
314
}
319
- return ans ;
315
+ return answer ;
320
316
}
321
317
}
322
318
```
You can’t perform that action at this time.
0 commit comments