LeetCode1734(2021.5.11)

LC1734. 解码异或后的排列

class Solution {
public:
    vector<int> decode(vector<int>& encoded) {
        int n = encoded.size() + 1, total = 0, odd = 0;
        for (int i = 1; i <= n; i++) total ^= i;
        for (int i = 1; i < n - 1; i += 2) odd ^= encoded[i];
        vector<int> perm(n);
        perm[0] = total ^ odd;
        for (int i = 0; i < n - 1; i++) perm[i + 1] = perm[i] ^ encoded[i];
        return perm;
    }
};

发表评论