74
74
75
75
时间复杂度 $O(n \times m)$,其中 $n$ 和 $m$ 分别是数组 ` nums ` 和 ` maximumBit ` 的值。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
76
76
77
+ ** 方法二:枚举优化**
78
+
79
+ 与方法一类似,我们先预处理出数组 ` nums ` 的异或和 $xs$,即 $xs=nums[ 0] \oplus nums[ 1] \oplus \cdots \oplus nums[ n-1] $。
80
+
81
+ 接下来,我们算出 $2^{maximumBit} - 1$,即 $2^{maximumBit}$ 减去 $1$,记为 $mask$。然后,我们从后往前枚举数组 ` nums ` 中的每个元素 $x$,当前的异或和为 $xs$,那么 $k=xs \oplus mask$ 就是每一次查询的答案。然后,我们将 $xs$ 更新为 $xs \oplus x$,继续枚举下一个元素。
82
+
83
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 ` nums ` 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
84
+
77
85
<!-- tabs:start -->
78
86
79
87
### ** Python3**
@@ -95,6 +103,19 @@ class Solution:
95
103
return ans
96
104
```
97
105
106
+ ``` python
107
+ class Solution :
108
+ def getMaximumXor (self , nums : List[int ], maximumBit : int ) -> List[int ]:
109
+ ans = []
110
+ xs = reduce (xor, nums)
111
+ mask = (1 << maximumBit) - 1
112
+ for x in nums[::- 1 ]:
113
+ k = xs ^ mask
114
+ ans.append(k)
115
+ xs ^= x
116
+ return ans
117
+ ```
118
+
98
119
### ** Java**
99
120
100
121
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -124,6 +145,27 @@ class Solution {
124
145
}
125
146
```
126
147
148
+ ``` java
149
+ class Solution {
150
+ public int [] getMaximumXor (int [] nums , int maximumBit ) {
151
+ int xs = 0 ;
152
+ for (int x : nums) {
153
+ xs ^ = x;
154
+ }
155
+ int mask = (1 << maximumBit) - 1 ;
156
+ int n = nums. length;
157
+ int [] ans = new int [n];
158
+ for (int i = 0 ; i < n; ++ i) {
159
+ int x = nums[n - i - 1 ];
160
+ int k = xs ^ mask;
161
+ ans[i] = k;
162
+ xs ^ = x;
163
+ }
164
+ return ans;
165
+ }
166
+ }
167
+ ```
168
+
127
169
### ** C++**
128
170
129
171
``` cpp
@@ -152,6 +194,28 @@ public:
152
194
};
153
195
```
154
196
197
+ ```cpp
198
+ class Solution {
199
+ public:
200
+ vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
201
+ int xs = 0;
202
+ for (int& x : nums) {
203
+ xs ^= x;
204
+ }
205
+ int mask = (1 << maximumBit) - 1;
206
+ int n = nums.size();
207
+ vector<int> ans(n);
208
+ for (int i = 0; i < n; ++i) {
209
+ int x = nums[n - i - 1];
210
+ int k = xs ^ mask;
211
+ ans[i] = k;
212
+ xs ^= x;
213
+ }
214
+ return ans;
215
+ }
216
+ };
217
+ ```
218
+
155
219
### ** Go**
156
220
157
221
``` go
@@ -175,6 +239,23 @@ func getMaximumXor(nums []int, maximumBit int) (ans []int) {
175
239
}
176
240
```
177
241
242
+ ``` go
243
+ func getMaximumXor (nums []int , maximumBit int ) (ans []int ) {
244
+ xs := 0
245
+ for _ , x := range nums {
246
+ xs ^= x
247
+ }
248
+ mask := (1 << maximumBit) - 1
249
+ for i := range nums {
250
+ x := nums[len (nums)-i-1 ]
251
+ k := xs ^ mask
252
+ ans = append (ans, k)
253
+ xs ^= x
254
+ }
255
+ return
256
+ }
257
+ ```
258
+
178
259
### ** TypeScript**
179
260
180
261
``` ts
@@ -200,6 +281,127 @@ function getMaximumXor(nums: number[], maximumBit: number): number[] {
200
281
}
201
282
```
202
283
284
+ ``` ts
285
+ function getMaximumXor(nums : number [], maximumBit : number ): number [] {
286
+ let xs = 0 ;
287
+ for (const x of nums ) {
288
+ xs ^= x ;
289
+ }
290
+ const mask = (1 << maximumBit ) - 1 ;
291
+ const n = nums .length ;
292
+ const ans = new Array (n );
293
+ for (let i = 0 ; i < n ; ++ i ) {
294
+ const x = nums [n - i - 1 ];
295
+ let k = xs ^ mask ;
296
+ ans [i ] = k ;
297
+ xs ^= x ;
298
+ }
299
+ return ans ;
300
+ }
301
+ ```
302
+
303
+ ### ** C#**
304
+
305
+ ``` cs
306
+ public class Solution {
307
+ public int [] GetMaximumXor (int [] nums , int maximumBit ) {
308
+ int xs = 0 ;
309
+ foreach (int x in nums ) {
310
+ xs ^= x ;
311
+ }
312
+ int n = nums .Length ;
313
+ int [] ans = new int [n ];
314
+ for (int i = 0 ; i < n ; ++ i ) {
315
+ int x = nums [n - i - 1 ];
316
+ int k = 0 ;
317
+ for (int j = maximumBit - 1 ; j >= 0 ; -- j ) {
318
+ if ((xs >> j & 1 ) == 0 ) {
319
+ k |= 1 << j ;
320
+ }
321
+ }
322
+ ans [i ] = k ;
323
+ xs ^= x ;
324
+ }
325
+ return ans ;
326
+ }
327
+ }
328
+ ```
329
+
330
+ ``` cs
331
+ public class Solution {
332
+ public int [] GetMaximumXor (int [] nums , int maximumBit ) {
333
+ int xs = 0 ;
334
+ foreach (int x in nums ) {
335
+ xs ^= x ;
336
+ }
337
+ int mask = (1 << maximumBit ) - 1 ;
338
+ int n = nums .Length ;
339
+ int [] ans = new int [n ];
340
+ for (int i = 0 ; i < n ; ++ i ) {
341
+ int x = nums [n - i - 1 ];
342
+ int k = xs ^ mask ;
343
+ ans [i ] = k ;
344
+ xs ^= x ;
345
+ }
346
+ return ans ;
347
+ }
348
+ }
349
+ ```
350
+
351
+ ### ** JavaScript**
352
+
353
+ ``` js
354
+ /**
355
+ * @param {number[]} nums
356
+ * @param {number} maximumBit
357
+ * @return {number[]}
358
+ */
359
+ var getMaximumXor = function (nums , maximumBit ) {
360
+ let xs = 0 ;
361
+ for (const x of nums) {
362
+ xs ^= x;
363
+ }
364
+ const n = nums .length ;
365
+ const ans = new Array (n);
366
+ for (let i = 0 ; i < n; ++ i) {
367
+ const x = nums[n - i - 1 ];
368
+ let k = 0 ;
369
+ for (let j = maximumBit - 1 ; j >= 0 ; -- j) {
370
+ if (((xs >> j) & 1 ) == 0 ) {
371
+ k |= 1 << j;
372
+ }
373
+ }
374
+ ans[i] = k;
375
+ xs ^= x;
376
+ }
377
+ return ans;
378
+ };
379
+ ```
380
+
381
+ ``` js
382
+ /**
383
+ * @param {number[]} nums
384
+ * @param {number} maximumBit
385
+ * @return {number[]}
386
+ */
387
+ var getMaximumXor = function (nums , maximumBit ) {
388
+ let xs = 0 ;
389
+ for (const x of nums) {
390
+ xs ^= x;
391
+ }
392
+ const mask = (1 << maximumBit) - 1 ;
393
+ const n = nums .length ;
394
+ const ans = new Array (n);
395
+ for (let i = 0 ; i < n; ++ i) {
396
+ const x = nums[n - i - 1 ];
397
+ let k = xs ^ mask;
398
+ ans[i] = k;
399
+ xs ^= x;
400
+ }
401
+ return ans;
402
+ };
403
+ ```
404
+
203
405
### ** ...**
204
406
205
407
```
0 commit comments