-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeXoredPermutation.java
More file actions
53 lines (46 loc) · 1.38 KB
/
DecodeXoredPermutation.java
File metadata and controls
53 lines (46 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package leetcode;
/**
* DecodeXoredPermutation
* https://leetcode-cn.com/problems/decode-xored-permutation/solution/
* 1734. 解码异或后的排列
* https://leetcode-cn.com/problems/decode-xored-permutation/solution/li-jie-liao-ti-yi-hou-jiu-suan-shi-yi-da-92lj/
* 注意: 读懂题目条件再做题
*
* @author tobin
* @since 2021-05-11
*/
public class DecodeXoredPermutation {
public static void main(String[] args) {
DecodeXoredPermutation sol = new DecodeXoredPermutation();
for (int i : sol.decode(new int[]{6, 5, 4, 6})) {
System.out.print(i + ", ");
}
System.out.println();
for (int i : sol.decode(new int[]{3, 1})) {
System.out.print(i + ", ");
}
System.out.println();
}
public int[] decode(int[] encoded) {
int n = encoded.length + 1;
// all xor sum
int total = 0;
for (int i = 1; i <= n; i++) {
total = total ^ i;
}
// get first
int a = total;
for (int i = 1; i < encoded.length; i += 2) {
a = a ^ encoded[i];
}
int[] result = new int[n];
// get others
int curr = a;
for (int i = 0; i < encoded.length; i++) {
result[i] = curr;
curr = curr ^ encoded[i];
}
result[encoded.length] = curr;
return result;
}
}