File tree 7 files changed +59
-38
lines changed
solution/2300-2399/2317.Maximum XOR After Operations
7 files changed +59
-38
lines changed Original file line number Diff line number Diff line change 44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
47
+ ** 方法一:位运算**
48
+
49
+ 在一次操作中,我们可以把 $nums[ i] $ 更新为 $nums[ i] \& (nums[ i] \oplus x)$,其中 $\& $ 表示逐位与运算,$\oplus$ 表示逐位异或运算。由于 $x$ 是任意非负整数,因此 $nums[ i] \oplus x$ 的结果是一个任意值,再与 $nums[ i] $ 逐位与运算,可以把 $nums[ i] $ 的二进制表示中的若干位 $1$ 变为 $0$。
50
+
51
+ 而题目中要获取的是 ` nums ` 所有元素的最大逐位异或和,对于一个二进制位,只要在 ` nums ` 中存在一个元素对应的二进制位为 $1$,那么这个二进制位对于最大逐位异或和的贡献就是 $1$。因此答案就是 ` nums ` 中所有元素的逐位或运算的结果。
52
+
53
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 ` nums ` 的长度。
54
+
47
55
<!-- tabs:start -->
48
56
49
57
### ** Python3**
53
61
``` python
54
62
class Solution :
55
63
def maximumXOR (self , nums : List[int ]) -> int :
56
- ans = 0
57
- for v in nums:
58
- ans |= v
59
- return ans
64
+ return reduce (or_, nums)
60
65
```
61
66
62
67
### ** Java**
@@ -67,8 +72,8 @@ class Solution:
67
72
class Solution {
68
73
public int maximumXOR (int [] nums ) {
69
74
int ans = 0 ;
70
- for (int v : nums) {
71
- ans |= v ;
75
+ for (int x : nums) {
76
+ ans |= x ;
72
77
}
73
78
return ans;
74
79
}
@@ -82,7 +87,9 @@ class Solution {
82
87
public:
83
88
int maximumXOR(vector<int >& nums) {
84
89
int ans = 0;
85
- for (int& v : nums) ans |= v;
90
+ for (int& x : nums) {
91
+ ans |= x;
92
+ }
86
93
return ans;
87
94
}
88
95
};
@@ -91,19 +98,24 @@ public:
91
98
### **Go**
92
99
93
100
```go
94
- func maximumXOR(nums []int) int {
95
- ans := 0
96
- for _, v := range nums {
97
- ans |= v
101
+ func maximumXOR(nums []int) (ans int) {
102
+ for _, x := range nums {
103
+ ans |= x
98
104
}
99
- return ans
105
+ return
100
106
}
101
107
```
102
108
103
109
### ** TypeScript**
104
110
105
111
``` ts
106
-
112
+ function maximumXOR(nums : number []): number {
113
+ let ans = 0 ;
114
+ for (const x of nums ) {
115
+ ans |= x ;
116
+ }
117
+ return ans ;
118
+ }
107
119
```
108
120
109
121
### ** ...**
Original file line number Diff line number Diff line change @@ -47,10 +47,7 @@ It can be shown that 11 is the maximum possible bitwise XOR.</pre>
47
47
``` python
48
48
class Solution :
49
49
def maximumXOR (self , nums : List[int ]) -> int :
50
- ans = 0
51
- for v in nums:
52
- ans |= v
53
- return ans
50
+ return reduce (or_, nums)
54
51
```
55
52
56
53
### ** Java**
@@ -59,8 +56,8 @@ class Solution:
59
56
class Solution {
60
57
public int maximumXOR (int [] nums ) {
61
58
int ans = 0 ;
62
- for (int v : nums) {
63
- ans |= v ;
59
+ for (int x : nums) {
60
+ ans |= x ;
64
61
}
65
62
return ans;
66
63
}
@@ -74,7 +71,9 @@ class Solution {
74
71
public:
75
72
int maximumXOR(vector<int >& nums) {
76
73
int ans = 0;
77
- for (int& v : nums) ans |= v;
74
+ for (int& x : nums) {
75
+ ans |= x;
76
+ }
78
77
return ans;
79
78
}
80
79
};
@@ -83,19 +82,24 @@ public:
83
82
### **Go**
84
83
85
84
```go
86
- func maximumXOR(nums []int) int {
87
- ans := 0
88
- for _, v := range nums {
89
- ans |= v
85
+ func maximumXOR(nums []int) (ans int) {
86
+ for _, x := range nums {
87
+ ans |= x
90
88
}
91
- return ans
89
+ return
92
90
}
93
91
```
94
92
95
93
### ** TypeScript**
96
94
97
95
``` ts
98
-
96
+ function maximumXOR(nums : number []): number {
97
+ let ans = 0 ;
98
+ for (const x of nums ) {
99
+ ans |= x ;
100
+ }
101
+ return ans ;
102
+ }
99
103
```
100
104
101
105
### ** ...**
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ class Solution {
2
2
public:
3
3
int maximumXOR (vector<int >& nums) {
4
4
int ans = 0 ;
5
- for (int & v : nums) ans |= v;
5
+ for (int & x : nums) {
6
+ ans |= x;
7
+ }
6
8
return ans;
7
9
}
8
10
};
Original file line number Diff line number Diff line change 1
- func maximumXOR (nums []int ) int {
2
- ans := 0
3
- for _ , v := range nums {
4
- ans |= v
1
+ func maximumXOR (nums []int ) (ans int ) {
2
+ for _ , x := range nums {
3
+ ans |= x
5
4
}
6
- return ans
5
+ return
7
6
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int maximumXOR (int [] nums ) {
3
3
int ans = 0 ;
4
- for (int v : nums ) {
5
- ans |= v ;
4
+ for (int x : nums ) {
5
+ ans |= x ;
6
6
}
7
7
return ans ;
8
8
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def maximumXOR (self , nums : List [int ]) -> int :
3
- ans = 0
4
- for v in nums :
5
- ans |= v
6
- return ans
3
+ return reduce (or_ , nums )
Original file line number Diff line number Diff line change
1
+ function maximumXOR ( nums : number [ ] ) : number {
2
+ let ans = 0 ;
3
+ for ( const x of nums ) {
4
+ ans |= x ;
5
+ }
6
+ return ans ;
7
+ }
You can’t perform that action at this time.
0 commit comments