diff --git a/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/README.md b/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/README.md index 01b852845..3f103b364 100755 --- a/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/README.md +++ b/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/README.md @@ -1,28 +1,31 @@ # [2425.Bitwise XOR of All Pairings][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two **0-indexed** arrays, `nums1` and `nums2`, consisting of non-negative integers. There exists another array, `nums3`, which contains the bitwise XOR of **all pairings** of integers between `nums1` and `nums2` (every integer in `nums1` is paired with every integer in `nums2` **exactly once**). + +Return the **bitwise XOR** of all integers in `nums3`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums1 = [2,1,3], nums2 = [10,2,5,0] +Output: 13 +Explanation: +A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3]. +The bitwise XOR of all these numbers is 13, so we return 13. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Bitwise XOR of All Pairings -```go ``` - +Input: nums1 = [1,2], nums2 = [3,4] +Output: 0 +Explanation: +All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0], +and nums1[1] ^ nums2[1]. +Thus, one possible nums3 array is [2,5,1,6]. +2 ^ 5 ^ 1 ^ 6 = 0, so we return 0. +``` ## 结语 diff --git a/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution.go b/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution.go index d115ccf5e..3ea2d35b7 100644 --- a/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution.go +++ b/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums1 []int, nums2 []int) int { + l1 := len(nums1) & 1 + l2 := len(nums2) & 1 + ans := 0 + if l2 == 1 { + for _, n := range nums1 { + ans ^= n + } + } + if l1 == 1 { + for _, n := range nums2 { + ans ^= n + } + } + return ans } diff --git a/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution_test.go b/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution_test.go index 14ff50eb4..8b44f0284 100644 --- a/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution_test.go +++ b/leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums1, nums2 []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 1, 3}, []int{10, 2, 5, 0}, 13}, + {"TestCase2", []int{1, 2}, []int{3, 4}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums1, c.nums2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums1, c.nums2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }