From f1823b49092a42fb13ff7e395f5e6e9e74095e80 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 23 Nov 2025 17:32:28 +0800 Subject: [PATCH] Add solution and test-cases for problem 2317 --- .../README.md | 36 +++++++++++++++++++ .../Solution.go | 8 +++-- .../Solution_test.go | 13 ++++--- 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 leetcode/2301-2400/2317.Maximum-XOR-After-Operations/README.md diff --git a/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/README.md b/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/README.md new file mode 100644 index 000000000..20ef1f301 --- /dev/null +++ b/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/README.md @@ -0,0 +1,36 @@ +# [2317.Maximum XOR After Operations ][title] + +## Description +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)`. + +Note that `AND` is the bitwise AND operation and `XOR` is the bitwise XOR operation. + +Return the **maximum** possible bitwise XOR of all elements of `nums` after applying the operation **any number** of times. + +**Example 1:** + +``` +Input: nums = [3,2,4,6] +Output: 7 +Explanation: Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2. +Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7. +It can be shown that 7 is the maximum possible bitwise XOR. +Note that other operations may be used to achieve a bitwise XOR of 7. +``` + +**Example 2:** + +``` +Input: nums = [1,2,3,9,2] +Output: 11 +Explanation: Apply the operation zero times. +The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11. +It can be shown that 11 is the maximum possible bitwise XOR. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/maximum-xor-after-operations/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution.go b/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution.go index d115ccf5e..34baf0e6d 100755 --- a/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution.go +++ b/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution.go @@ -1,5 +1,9 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + var ret int + for _, n := range nums { + ret |= n + } + return ret } diff --git a/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution_test.go b/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution_test.go index 14ff50eb4..9795c5886 100755 --- a/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution_test.go +++ b/leetcode/2301-2400/2317.Maximum-XOR-After-Operations/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 2, 4, 6}, 7}, + {"TestCase2", []int{1, 2, 3, 9, 2}, 11}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }