Skip to content

Commit e766e63

Browse files
committed
Add solution and test-cases for problem 2683
1 parent d569e0c commit e766e63

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [2683.Neighboring Bitwise XOR][title]
2+
3+
## Description
4+
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`.
5+
6+
Specifically, for each index `i` in the range `[0, n - 1]`:
7+
8+
- If `i = n - 1`, then `derived[i] = original[i] ⊕ original[0]`.
9+
- Otherwise, `derived[i] = original[i] ⊕ original[i + 1]`.
10+
11+
Given an array `derived`, your task is to determine whether there exists a **valid binary array** `original` that could have formed `derived`.
12+
13+
Return **true** if such an array exists or **false** otherwise.
14+
15+
- A binary array is an array containing only **0**'s and **1**'s
16+
17+
**Example 1:**
18+
19+
```
20+
Input: derived = [1,1,0]
21+
Output: true
22+
Explanation: A valid original array that gives derived is [0,1,0].
23+
derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1
24+
derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1
25+
derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: derived = [1,1]
32+
Output: true
33+
Explanation: A valid original array that gives derived is [0,1].
34+
derived[0] = original[0] ⊕ original[1] = 1
35+
derived[1] = original[1] ⊕ original[0] = 1
36+
```
37+
38+
**Example 3:**
39+
40+
```
41+
Input: derived = [1,0]
42+
Output: false
43+
Explanation: There is no valid original array that gives derived.
44+
```
45+
46+
## 结语
47+
48+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
49+
50+
[title]: https://leetcode.com/problems/neighboring-bitwise-xor
51+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(derived []int) bool {
4+
ans := 0
5+
for _, n := range derived {
6+
ans ^= n
7+
}
8+
return ans == 0
59
}

leetcode/2601-2700/2683.Neighboring-Bitwise-XOR/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 1, 0}, true},
17+
{"TestCase2", []int{1, 1}, true},
18+
{"TestCase3", []int{1, 0}, false},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)