文章目录
- 题目
- 标题和出处
- 难度
- 题目描述
- 要求
- 示例
- 数据范围
- 解法
- 思路和算法
- 代码
- 复杂度分析
题目
标题和出处
标题:解码异或后的排列
出处:1734. 解码异或后的排列
难度
6 级
题目描述
要求
有一个整数数组 perm \texttt{perm} perm,是前 n \texttt{n} n 个正整数的排列,且 n \texttt{n} n 是奇数。
它被加密成另一个长度为 n − 1 \texttt{n} - \texttt{1} n−1 的整数数组 encoded \texttt{encoded} encoded,满足 encoded[i] = perm[i] ⊕ perm[i + 1] \texttt{encoded[i] = perm[i]} \oplus \texttt{perm[i + 1]} encoded[i] = perm[i]⊕perm[i + 1]。例如,如果 perm = [1,3,2] \texttt{perm = [1,3,2]} perm = [1,3,2],那么 encoded = [2,1] \texttt{encoded = [2,1]} encoded = [2,1]。
给定 encoded \texttt{encoded} encoded 数组,返回原始数组 perm \texttt{perm} perm。题目保证答案存在且唯一。
示例
示例 1:
输入:
encoded
=
[3,1]
\texttt{encoded = [3,1]}
encoded = [3,1]
输出:
[1,2,3]
\texttt{[1,2,3]}
[1,2,3]
解释:如果
perm
=
[1,2,3]
\texttt{perm = [1,2,3]}
perm = [1,2,3],那么
encoded
=
[1
⊕
2,2
⊕
3]
=
[3,1]
\texttt{encoded = [1} \oplus \texttt{2,2} \oplus \texttt{3] = [3,1]}
encoded = [1⊕2,2⊕3] = [3,1]。
示例 2:
输入:
encoded
=
[6,5,4,6]
\texttt{encoded = [6,5,4,6]}
encoded = [6,5,4,6]
输出:
[2,4,1,5,3]
\texttt{[2,4,1,5,3]}
[2,4,1,5,3]
数据范围
- 3 ≤ n < 10 5 \texttt{3} \le \texttt{n} < \texttt{10}^\texttt{5} 3≤n<105
- n \texttt{n} n 是奇数
- encoded.length = n − 1 \texttt{encoded.length} = \texttt{n} - \texttt{1} encoded.length=n−1
解法
思路和算法
对于 0 ≤ i < n − 1 0 \le i < n - 1 0≤i<n−1 有 encoded [ i ] = perm [ i ] ⊕ perm [ i + 1 ] \textit{encoded}[i] = \textit{perm}[i] \oplus \textit{perm}[i + 1] encoded[i]=perm[i]⊕perm[i+1],利用按位异或的性质可以得到 encoded [ i ] ⊕ perm [ i ] = perm [ i + 1 ] ⊕ perm [ i ] ⊕ perm [ i ] = perm [ i + 1 ] \textit{encoded}[i] \oplus \textit{perm}[i] = \textit{perm}[i + 1] \oplus \textit{perm}[i] \oplus \textit{perm}[i] = \textit{perm}[i + 1] encoded[i]⊕perm[i]=perm[i+1]⊕perm[i]⊕perm[i]=perm[i+1]。因此对于 1 ≤ i < n 1 \le i < n 1≤i<n 有 perm [ i ] = encoded [ i − 1 ] ⊕ perm [ i − 1 ] \textit{perm}[i] = \textit{encoded}[i - 1] \oplus \textit{perm}[i - 1] perm[i]=encoded[i−1]⊕perm[i−1]。如果可以知道 perm [ 0 ] \textit{perm}[0] perm[0] 的值,则可以解码得到完整的原始数组 perm \textit{perm} perm。
由于数组 perm \textit{perm} perm 的长度 n n n 是奇数,因此除了 perm [ 0 ] \textit{perm}[0] perm[0] 以外的剩余元素个数 n − 1 n - 1 n−1 是偶数,数组 encoded \textit{encoded} encoded 的长度 n − 1 n - 1 n−1 是偶数。可以在数组 encoded \textit{encoded} encoded 中寻找 n − 1 2 \dfrac{n - 1}{2} 2n−1 个元素,使得这些元素的异或结果为 perm [ 1 ] \textit{perm}[1] perm[1] 到 perm [ n − 1 ] \textit{perm}[n - 1] perm[n−1] 的异或结果。
由于 encoded [ i ] = perm [ i ] ⊕ perm [ i + 1 ] \textit{encoded}[i] = \textit{perm}[i] \oplus \textit{perm}[i + 1] encoded[i]=perm[i]⊕perm[i+1],因此计算 encoded \textit{encoded} encoded 的所有奇数下标处的元素的异或结果,记为 oddXor \textit{oddXor} oddXor,则该异或结果为 encoded \textit{encoded} encoded 的 n − 1 2 \dfrac{n - 1}{2} 2n−1 个元素的异或结果,等于 perm [ 1 ] \textit{perm}[1] perm[1] 到 perm [ n − 1 ] \textit{perm}[n - 1] perm[n−1] 的异或结果。
用 totalXor \textit{totalXor} totalXor 表示 1 1 1 到 n n n 的所有整数的异或结果,则 totalXor = oddXor ⊕ perm [ 0 ] \textit{totalXor} = \textit{oddXor} \oplus \textit{perm}[0] totalXor=oddXor⊕perm[0],利用按位异或的性质可以得到 totalXor ⊕ oddXor = oddXor ⊕ oddXor ⊕ perm [ 0 ] = perm [ 0 ] \textit{totalXor} \oplus \textit{oddXor} = \textit{oddXor} \oplus \textit{oddXor} \oplus \textit{perm}[0] = \textit{perm}[0] totalXor⊕oddXor=oddXor⊕oddXor⊕perm[0]=perm[0],即 perm [ 0 ] \textit{perm}[0] perm[0] 的值等于 totalXor \textit{totalXor} totalXor 和 oddXor \textit{oddXor} oddXor 的异或结果。
得到 perm [ 0 ] \textit{perm}[0] perm[0] 的值之后,从 i = 1 i = 1 i=1 到 i = n − 1 i = n - 1 i=n−1 依次计算 perm [ i ] \textit{perm}[i] perm[i] 的值,即可得到原数组 perm \textit{perm} perm,且原数组的结果唯一。
代码
class Solution {
public int[] decode(int[] encoded) {
int n = encoded.length + 1;
int[] perm = new int[n];
int totalXor = 0;
for (int i = 1; i <= n; i++) {
totalXor ^= i;
}
int oddXor = 0;
for (int i = 1; i < n; i += 2) {
oddXor ^= encoded[i];
}
perm[0] = totalXor ^ oddXor;
for (int i = 1; i < n; i++) {
perm[i] = encoded[i - 1] ^ perm[i - 1];
}
return perm;
}
}
复杂度分析
-
时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 perm \textit{perm} perm 的长度。需要遍历 1 1 1 到 n n n 的所有整数计算总异或值,然后遍历长度为 n − 1 n - 1 n−1 的数组 encoded \textit{encoded} encoded 两次得到原始数组 perm \textit{perm} perm。
-
空间复杂度: O ( 1 ) O(1) O(1)。注意返回值不计入空间复杂度。