Skip to content

Commit f38bff5

Browse files
authored
Merge pull request 6boris#1294 from 0xff-dev/1835
Add solution and test-cases for problem 1835
2 parents a381f72 + 23c5818 commit f38bff5

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1835.Find XOR Sum of All Pairs Bitwise AND][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+
The **XOR sum** of a list is the bitwise `XOR` of all its elements. If the list only contains one element, then its **XOR sum** will be equal to this element.
5+
6+
- For example, the **XOR sum** of `[1,2,3,4]` is equal to `1 XOR 2 XOR 3 XOR 4 = 4`, and the **XOR sum** of `[3]` is equal to `3`.
7+
8+
You are given two **0-indexed** arrays `arr1` and `arr2` that consist only of non-negative integers.
9+
10+
Consider the list containing the result of `arr1[i] AND arr2[j]` (bitwise AND) for every `(i, j)` pair where `0 <= i < arr1.length` and `0 <= j < arr2.length`.
11+
12+
Return the **XOR sum** of the aforementioned list.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: arr1 = [1,2,3], arr2 = [6,5]
18+
Output: 0
19+
Explanation: The list = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1].
20+
The XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.
1321
```
1422

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

20-
### 思路1
21-
> ...
22-
Find XOR Sum of All Pairs Bitwise AND
23-
```go
2425
```
25-
26+
Input: arr1 = [12], arr2 = [4]
27+
Output: 4
28+
Explanation: The list = [12 AND 4] = [4]. The XOR sum = 4.
29+
```
2630

2731
## 结语
2832

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(arr1 []int, arr2 []int) int {
4+
xor2 := 0
5+
for _, n := range arr2 {
6+
xor2 ^= n
7+
}
8+
ans := 0
9+
for _, n := range arr1 {
10+
ans ^= (n & xor2)
11+
}
12+
return ans
513
}

leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/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+
arr1, arr2 []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3}, []int{6, 5}, 0},
17+
{"TestCase2", []int{12}, []int{4}, 4},
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.arr1, c.arr2)
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.arr1, c.arr2)
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)