Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f7cf05e

Browse files
committed
feat: add solutions to lc problem: No.2317
No.2317.Maximum XOR After Operations
1 parent ca38f01 commit f7cf05e

File tree

7 files changed

+59
-38
lines changed

7 files changed

+59
-38
lines changed

‎solution/2300-2399/2317.Maximum XOR After Operations/README.md‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

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+
4755
<!-- tabs:start -->
4856

4957
### **Python3**
@@ -53,10 +61,7 @@
5361
```python
5462
class Solution:
5563
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)
6065
```
6166

6267
### **Java**
@@ -67,8 +72,8 @@ class Solution:
6772
class Solution {
6873
public int maximumXOR(int[] nums) {
6974
int ans = 0;
70-
for (int v : nums) {
71-
ans |= v;
75+
for (int x : nums) {
76+
ans |= x;
7277
}
7378
return ans;
7479
}
@@ -82,7 +87,9 @@ class Solution {
8287
public:
8388
int maximumXOR(vector<int>& nums) {
8489
int ans = 0;
85-
for (int& v : nums) ans |= v;
90+
for (int& x : nums) {
91+
ans |= x;
92+
}
8693
return ans;
8794
}
8895
};
@@ -91,19 +98,24 @@ public:
9198
### **Go**
9299
93100
```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
98104
}
99-
return ans
105+
return
100106
}
101107
```
102108

103109
### **TypeScript**
104110

105111
```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+
}
107119
```
108120

109121
### **...**

‎solution/2300-2399/2317.Maximum XOR After Operations/README_EN.md‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ It can be shown that 11 is the maximum possible bitwise XOR.</pre>
4747
```python
4848
class Solution:
4949
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)
5451
```
5552

5653
### **Java**
@@ -59,8 +56,8 @@ class Solution:
5956
class Solution {
6057
public int maximumXOR(int[] nums) {
6158
int ans = 0;
62-
for (int v : nums) {
63-
ans |= v;
59+
for (int x : nums) {
60+
ans |= x;
6461
}
6562
return ans;
6663
}
@@ -74,7 +71,9 @@ class Solution {
7471
public:
7572
int maximumXOR(vector<int>& nums) {
7673
int ans = 0;
77-
for (int& v : nums) ans |= v;
74+
for (int& x : nums) {
75+
ans |= x;
76+
}
7877
return ans;
7978
}
8079
};
@@ -83,19 +82,24 @@ public:
8382
### **Go**
8483
8584
```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
9088
}
91-
return ans
89+
return
9290
}
9391
```
9492

9593
### **TypeScript**
9694

9795
```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+
}
99103
```
100104

101105
### **...**

‎solution/2300-2399/2317.Maximum XOR After Operations/Solution.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
int maximumXOR(vector<int>& nums) {
44
int ans = 0;
5-
for (int& v : nums) ans |= v;
5+
for (int& x : nums) {
6+
ans |= x;
7+
}
68
return ans;
79
}
810
};
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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
54
}
6-
returnans
5+
return
76
}

‎solution/2300-2399/2317.Maximum XOR After Operations/Solution.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public int maximumXOR(int[] nums) {
33
int ans = 0;
4-
for (int v : nums) {
5-
ans |= v;
4+
for (int x : nums) {
5+
ans |= x;
66
}
77
return ans;
88
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
class Solution:
22
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)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function maximumXOR(nums: number[]): number {
2+
let ans = 0;
3+
for (const x of nums) {
4+
ans |= x;
5+
}
6+
return ans;
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /