Skip to content

Commit f1823b4

Browse files
committed
Add solution and test-cases for problem 2317
1 parent f03226b commit f1823b4

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [2317.Maximum XOR After Operations ][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums`. In one operation, select **any** non-negative integer `x` and an index `i`, then **update** `nums[i]` to be equal to `nums[i] AND (nums[i] XOR x)`.
5+
6+
Note that `AND` is the bitwise AND operation and `XOR` is the bitwise XOR operation.
7+
8+
Return the **maximum** possible bitwise XOR of all elements of `nums` after applying the operation **any number** of times.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [3,2,4,6]
14+
Output: 7
15+
Explanation: Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
16+
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
17+
It can be shown that 7 is the maximum possible bitwise XOR.
18+
Note that other operations may be used to achieve a bitwise XOR of 7.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: nums = [1,2,3,9,2]
25+
Output: 11
26+
Explanation: Apply the operation zero times.
27+
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
28+
It can be shown that 11 is the maximum possible bitwise XOR.
29+
```
30+
31+
## 结语
32+
33+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
34+
35+
[title]: https://leetcode.com/problems/maximum-xor-after-operations/
36+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
var ret int
5+
for _, n := range nums {
6+
ret |= n
7+
}
8+
return ret
59
}

leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 2, 4, 6}, 7},
17+
{"TestCase2", []int{1, 2, 3, 9, 2}, 11},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)