41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
+ ** 方法一:位运算**
45
+
46
+ 我们注意到,数组 $perm$ 是前 $n$ 个正整数的排列,因此 $perm$ 的所有元素的异或和为 $1 \oplus 2 \oplus \cdots \oplus n$,记为 $a$。而 $encode[ i] =perm[ i] \oplus perm[ i+1] $,如果我们将 $encode[ 0] ,encode[ 2] ,\cdots,encode[ n-3] $ 的所有元素的异或和记为 $b$,则 $perm[ n-1] =a \oplus b$。知道了 $perm$ 的最后一个元素,我们就可以通过逆序遍历数组 $encode$ 求出 $perm$ 的所有元素。
47
+
48
+ 时间复杂度 $O(n)$,其中 $n$ 为数组 $perm$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
49
+
44
50
<!-- tabs:start -->
45
51
46
52
### ** Python3**
@@ -54,12 +60,13 @@ class Solution:
54
60
a = b = 0
55
61
for i in range (0 , n - 1 , 2 ):
56
62
a ^= encoded[i]
57
- for i in range (n + 1 ):
63
+ for i in range (1 , n + 1 ):
58
64
b ^= i
59
- ans = [a ^ b]
60
- for e in encoded[::- 1 ]:
61
- ans.append(ans[- 1 ] ^ e)
62
- return ans[::- 1 ]
65
+ perm = [0 ] * n
66
+ perm[- 1 ] = a ^ b
67
+ for i in range (n - 2 , - 1 , - 1 ):
68
+ perm[i] = encoded[i] ^ perm[i + 1 ]
69
+ return perm
63
70
```
64
71
65
72
### ** Java**
@@ -68,23 +75,21 @@ class Solution:
68
75
69
76
``` java
70
77
class Solution {
71
-
72
78
public int [] decode (int [] encoded ) {
73
79
int n = encoded. length + 1 ;
74
- int [] ans = new int [n];
75
- int a = 0 ;
76
- int b = 0 ;
80
+ int a = 0 , b = 0 ;
77
81
for (int i = 0 ; i < n - 1 ; i += 2 ) {
78
82
a ^ = encoded[i];
79
83
}
80
- for (int i = 0 ; i < n + 1 ; ++ i) {
84
+ for (int i = 1 ; i <= n ; ++ i) {
81
85
b ^ = i;
82
86
}
83
- ans[n - 1 ] = a ^ b;
87
+ int [] perm = new int [n];
88
+ perm[n - 1 ] = a ^ b;
84
89
for (int i = n - 2 ; i >= 0 ; -- i) {
85
- ans [i] = ans[i + 1 ] ^ encoded[i ];
90
+ perm [i] = encoded[i ] ^ perm[i + 1 ];
86
91
}
87
- return ans ;
92
+ return perm ;
88
93
}
89
94
}
90
95
```
@@ -96,13 +101,19 @@ class Solution {
96
101
public:
97
102
vector<int > decode(vector<int >& encoded) {
98
103
int n = encoded.size() + 1;
99
- vector<int > ans(n);
100
104
int a = 0, b = 0;
101
- for (int i = 0; i < n - 1; i += 2) a ^= encoded[ i] ;
102
- for (int i = 0; i < n + 1; ++i) b ^= i;
103
- ans[ n - 1] = a ^ b;
104
- for (int i = n - 2; i >= 0; --i) ans[ i] = ans[ i + 1] ^ encoded[ i] ;
105
- return ans;
105
+ for (int i = 0; i < n - 1; i += 2) {
106
+ a ^= encoded[ i] ;
107
+ }
108
+ for (int i = 1; i <= n; ++i) {
109
+ b ^= i;
110
+ }
111
+ vector<int > perm(n);
112
+ perm[ n - 1] = a ^ b;
113
+ for (int i = n - 2; ~ i; --i) {
114
+ perm[ i] = encoded[ i] ^ perm[ i + 1] ;
115
+ }
116
+ return perm;
106
117
}
107
118
};
108
119
```
@@ -112,19 +123,19 @@ public:
112
123
```go
113
124
func decode(encoded []int) []int {
114
125
n := len(encoded) + 1
115
- ans := make([]int, n)
116
126
a, b := 0, 0
117
127
for i := 0; i < n-1; i += 2 {
118
128
a ^= encoded[i]
119
129
}
120
- for i := 0 ; i < n+1 ; i++ {
130
+ for i := 1 ; i <= n ; i++ {
121
131
b ^= i
122
132
}
123
- ans[n-1] = a ^ b
133
+ perm := make([]int, n)
134
+ perm[n-1] = a ^ b
124
135
for i := n - 2; i >= 0; i-- {
125
- ans [i] = ans[i+1 ] ^ encoded[i ]
136
+ perm [i] = encoded[i ] ^ perm[i+1 ]
126
137
}
127
- return ans
138
+ return perm
128
139
}
129
140
```
130
141
0 commit comments