diff --git a/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/README.md b/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/README.md index 4b3e31ca3..c912056f3 100755 --- a/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/README.md +++ b/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/README.md @@ -1,28 +1,32 @@ # [1835.Find XOR Sum of All Pairs Bitwise AND][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 +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. + +- 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`. + +You are given two **0-indexed** arrays `arr1` and `arr2` that consist only of non-negative integers. + +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`. + +Return the **XOR sum** of the aforementioned list. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: arr1 = [1,2,3], arr2 = [6,5] +Output: 0 +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]. +The XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find XOR Sum of All Pairs Bitwise AND -```go ``` - +Input: arr1 = [12], arr2 = [4] +Output: 4 +Explanation: The list = [12 AND 4] = [4]. The XOR sum = 4. +``` ## 结语 diff --git a/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution.go b/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution.go index d115ccf5e..b1180f4ab 100644 --- a/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution.go +++ b/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution.go @@ -1,5 +1,13 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(arr1 []int, arr2 []int) int { + xor2 := 0 + for _, n := range arr2 { + xor2 ^= n + } + ans := 0 + for _, n := range arr1 { + ans ^= (n & xor2) + } + return ans } diff --git a/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution_test.go b/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution_test.go index 14ff50eb4..dc6b39d7c 100644 --- a/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution_test.go +++ b/leetcode/1801-1900/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + arr1, arr2 []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3}, []int{6, 5}, 0}, + {"TestCase2", []int{12}, []int{4}, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.arr1, c.arr2) 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.arr1, c.arr2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }