Skip to content

Commit d2eedfc

Browse files
authored
Merge pull request #1096 from 0xff-dev/2425
Add solution and test-cases for problem 2425
2 parents 94867d4 + 2484b86 commit d2eedfc

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [2425.Bitwise XOR of All Pairings][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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**).
5+
6+
Return the **bitwise XOR** of all integers in `nums3`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums1 = [2,1,3], nums2 = [10,2,5,0]
12+
Output: 13
13+
Explanation:
14+
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
15+
The bitwise XOR of all these numbers is 13, so we return 13.
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Bitwise XOR of All Pairings
23-
```go
2420
```
25-
21+
Input: nums1 = [1,2], nums2 = [3,4]
22+
Output: 0
23+
Explanation:
24+
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
25+
and nums1[1] ^ nums2[1].
26+
Thus, one possible nums3 array is [2,5,1,6].
27+
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
28+
```
2629

2730
## 结语
2831

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums1 []int, nums2 []int) int {
4+
l1 := len(nums1) & 1
5+
l2 := len(nums2) & 1
6+
ans := 0
7+
if l2 == 1 {
8+
for _, n := range nums1 {
9+
ans ^= n
10+
}
11+
}
12+
if l1 == 1 {
13+
for _, n := range nums2 {
14+
ans ^= n
15+
}
16+
}
17+
return ans
518
}

leetcode/2401-2500/2425.Bitwise-XOR-of-All-Pairings/Solution_test.go

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

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.nums1, c.nums2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.nums1, c.nums2)
2827
}
2928
})
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)