Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2109786

Browse files
committed
Merge pull request #339 from 0xff-dev/1734
Add solution and test-cases for problem 1734
2 parents 29eff80 + 26b2da9 commit 2109786

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

‎leetcode/1701-1800/1734.Decode-XORed-Permutation/README.md‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# [1734.Decode XORed Permutation][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+
There is an integer array `perm` that is a permutation of the first `n` positive integers, where `n` is always `odd`.
5+
6+
It was encoded into another integer array `encoded` of length `n - 1`, such that `encoded[i] = perm[i] XOR perm[i + 1]`. For example, if `perm = [1,3,2]`, then `encoded = [2,1]`.
7+
8+
Given the `encoded` array, return the original array `perm`. It is guaranteed that the answer exists and is unique.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: encoded = [3,1]
14+
Output: [1,2,3]
15+
Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Decode XORed Permutation
23-
```go
2420
```
25-
21+
Input: encoded = [6,5,4,6]
22+
Output: [2,4,1,5,3]
23+
```
2624

2725
## 结语
2826

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

3-
func Solution(x bool) bool {
4-
return x
3+
func xor(n int) int {
4+
switch n % 4 {
5+
case 1:
6+
return 1
7+
case 0:
8+
return n
9+
case 2:
10+
return n - 1
11+
}
12+
return 0
13+
}
14+
func Solution(encoded []int) []int {
15+
l := len(encoded)
16+
toN := xor(l + 1)
17+
pick := 0
18+
for step := 0; step < l; step += 2 {
19+
pick ^= encoded[step]
20+
}
21+
22+
lastElem := toN ^ pick
23+
ans := make([]int, l+1)
24+
ans[l] = lastElem
25+
for idx := l - 1; idx >= 0; idx-- {
26+
ans[idx] = ans[idx+1] ^ encoded[idx]
27+
}
28+
return ans
529
}

‎leetcode/1701-1800/1734.Decode-XORed-Permutation/Solution_test.go‎

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
//压力测试
32+
//压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
//使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /