LeetCode 1734. Decode XORed Permutation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> decode(vector<int>& encoded) {
int n = encoded.size() + 1;
int total = 0;
for (int i = 1; i <= n; ++i)
total ^= i;
int odd = 0;
for (int i = 1; i < n - 1; i += 2)
odd ^= encoded[i];
vector<int> perm(n);
perm[0] = total ^ odd;
for (int i = 1; i < n; ++i)
perm[i] = perm[i - 1] ^ encoded[i - 1];
return perm;
}
};