forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeXoredPermutation.cpp
121 lines (103 loc) · 3.31 KB
/
DecodeXoredPermutation.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Source : https://leetcode.com/problems/decode-xored-permutation/
// Author : Hao Chen
// Date : 2021-02-17
/*****************************************************************************************************
*
* There is an integer array perm that is a permutation of the first n positive integers, where n is
* always odd.
*
* 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].
*
* Given the encoded array, return the original array perm. It is guaranteed that the answer exists
* and is unique.
*
* Example 1:
*
* Input: encoded = [3,1]
* Output: [1,2,3]
* Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
*
* Example 2:
*
* Input: encoded = [6,5,4,6]
* Output: [2,4,1,5,3]
*
* Constraints:
*
* 3 <= n < 105
* n is odd.
* encoded.length == n - 1
******************************************************************************************************/
/*
XOR Basic Rules
---------------
At first, we need understand the following XOR rules
x^x = 0
0^x = x
with the rule 1 and 2 , we can have: x^x^y = y . the element ocurrs twice would be removed.
if x^y = z , then x = y^z and y = x^z
XOR from 1 to n
---------------
Secondly, suppose the f(n) is XOR from 1 to n , f(n) = 1^2^3^4^5 .... ^n .
if the n is odd number, then we can observse the following things:
f(1) = 1
f(3) = 1^2^3 = 0
f(5) = 1^2^3^4^5 = 1
f(7) = 1^2^3^4^5^6^7 = 0
f(9) = 1^2^3^4^5^6^7^8^9 = 1
... ...
so, f(n) = [ (n-1)/2 + 1 ] % 2 - (n is odd number)
Solution
--------
We know,
encode = { (p1^p2), (p2^p3), (p3^p4), ... } - p is the permutation array
so, if xor the odd index of encoded[] array,
encoded[1] = p2 ^ p3;
encoded[3] = p4 ^ p5;
encoded[5] = p6 ^ p7;
......
we can get: f(m) = p2 ^ p3 ^ p4 ...pn, without the p1
with the XOR rule 3, we know
p1 = f(n) ^ f(m)
with the XOR rule 4, we know encoded[0] = p1^p2 then
p2 = p1 ^ enclode[0]
So,
p[i+1] = p[i] ^ enclode[i]
*/
class Solution {
public:
vector<int> decode(vector<int>& encoded) {
// f(n) = 1^2^3^4^5 .... ^n ( n is odd number)
// then,
// f(3) = 1
// f(5) = 0
// f(7) = 1
// f(9) = 0
// ... ...
// f(n) = [ (n-1)/2 + 1 ] % 2
int nxor = (encoded.size() / 2 + 1) % 2 ;
// suppose the a[1], a[2], a[3] .... a[n] is the result array.
// xor the odd index of encoded[] array,
// encoded[1] = a[2] ^ a[3];
// encoded[3] = a[4] ^ a[5];
// encode [5] = a[6] ^ a[7];
// ......
// we can get: f(m) = a[2] ^ a[3] ^ a[4] ...a[n]
int mxor = encoded[1];
for (int i=3; i< encoded.size(); i+=2) {
mxor ^= encoded[i];
}
// a[1] = f(n) ^ f(m)
x = nxor ^ mxor;
vector<int> result;
result.push_back(x);
// we know, encoded[i] = a[i] ^ a[i+1]
// so, a[i+1] = a[i] ^ encoded[i]
for (int i=0; i< encoded.size(); i++) {
x = x ^ encoded[i];
result.push_back(x);
}
return result;
}
};