Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/README.md
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 6 additions & 6 deletions leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading