From e766e63d5be69b811fb57f5d99289b22d76424cb Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 17 Jan 2025 09:09:18 +0800 Subject: [PATCH] Add solution and test-cases for problem 2683 --- .../2683.Neighboring-Bitwise-XOR/README.md | 51 +++++++++++++++++++ .../2683.Neighboring-Bitwise-XOR/Solution.go | 8 ++- .../Solution_test.go | 12 ++--- 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/README.md diff --git a/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/README.md b/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/README.md new file mode 100644 index 000000000..07377a32f --- /dev/null +++ b/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/README.md @@ -0,0 +1,51 @@ +# [2683.Neighboring Bitwise XOR][title] + +## Description +A **0-indexed** array `derived` with length n is derived by computing the **bitwise XOR** (⊕) of adjacent values in a **binary array** `original` of length `n`. + +Specifically, for each index `i` in the range `[0, n - 1]`: + +- If `i = n - 1`, then `derived[i] = original[i] ⊕ original[0]`. +- Otherwise, `derived[i] = original[i] ⊕ original[i + 1]`. + +Given an array `derived`, your task is to determine whether there exists a **valid binary array** `original` that could have formed `derived`. + +Return **true** if such an array exists or **false** otherwise. + +- A binary array is an array containing only **0**'s and **1**'s + +**Example 1:** + +``` +Input: derived = [1,1,0] +Output: true +Explanation: A valid original array that gives derived is [0,1,0]. +derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1 +derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1 +derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0 +``` + +**Example 2:** + +``` +Input: derived = [1,1] +Output: true +Explanation: A valid original array that gives derived is [0,1]. +derived[0] = original[0] ⊕ original[1] = 1 +derived[1] = original[1] ⊕ original[0] = 1 +``` + +**Example 3:** + +``` +Input: derived = [1,0] +Output: false +Explanation: There is no valid original array that gives derived. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/neighboring-bitwise-xor +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution.go b/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution.go index d115ccf5e..1b5c28ca9 100755 --- a/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution.go +++ b/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution.go @@ -1,5 +1,9 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(derived []int) bool { + ans := 0 + for _, n := range derived { + ans ^= n + } + return ans == 0 } diff --git a/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution_test.go b/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution_test.go index 14ff50eb4..91878a2d0 100755 --- a/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution_test.go +++ b/leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs []int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 1, 0}, true}, + {"TestCase2", []int{1, 1}, true}, + {"TestCase3", []int{1, 0}, false}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }