【bit operation】

foreword

        There is no
       knowledge point for today: Bit operations
        need to perform bit operations today, but it feels more like a math test.

1. Topic

topic difficulty
868. Binary Spacing ⭐️
1734. Permutation after decoding XOR ⭐️
89. Gray coding ⭐️
1238. Cyclic Code Arrangement ⭐️

2. Algorithm idea

1. Binary spacing

        (1) [Simulation] If the lowest bit of binary n is 1, then judge whether it is the first one, if not, update the answer; then update pos to record the position of the last 1.
        Time complexity: O ( log ⁡ n ) O(\log{n})O(logn)

class Solution {
    
    
public:
    int binaryGap(int n) {
    
    
        int ans = 0, pos = -1;
        for (int i = 0; n; ++ i) {
    
    
            if (n & 1) {
    
    
                if (pos != -1) ans = max(ans, i - pos);
                pos = i;
            }
            n >>= 1;
        }
        return ans;
    }
};

        (2) [Binary bit simulation] int has a total of 32 bits, judge whether each bit is 1, if not, skip; if yes, judge whether it is the first, if not, update the answer, and then update pos to record the current position of 1.
        Time complexity: O ( 32 ) O(32)O(32)

class Solution {
    
    
public:
    int binaryGap(int n) {
    
    
        int ans = 0, pos = -1;
        for (int i = 31; i >= 0; -- i) {
    
    
            if (((n >> i) & 1) == 1) {
    
    
                if (pos != -1) ans = max(ans, pos - i);
                pos = i;
            }
        }
        return ans;
    }
};

2. Arrangement after decoding XOR

        (1) [Simulation] There are two important conditions, the original array is the arrangement of the first n numbers, and the length of the encrypted array is n - 1. Then according to XOR XORThe characteristics of XOR , we can first seek from1 − n 1-n1The XOR result of n , and then according to the characteristics of the encoded array, the length is an odd number and is the result of the XOR of two adjacent elements of the element group; we perform the XOR operation on the encoded array with an element interval, and then we can get the first element Or the value of the last element, and finally restore the original array in turn according to the encoded characteristics.
        Time complexity:O ( n ) O(n)O ( n )

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

3. Gray coding

        (1) [Symmetric generation] For the answer array, the flipped +1 arrays are spliced ​​together, and the original array is shifted one bit to the left. For the last digit, it is the same as the first digit before adding 1, and there is only one difference between the last digit and the last digit after +1.
        Time complexity: O ( n ) O(n)O ( n )

class Solution {
    
    
public:
    vector<int> grayCode(int n) {
    
    
        vector<int> ans;
        ans.push_back(0);
        while (n -- > 0) {
    
    
            int m = ans.size();
            for (int i = m - 1; i >= 0; -- i) {
    
    
                ans[i] <<= 1;
                ans.push_back(ans[i] + 1);
            }
        }
        return ans;
    }
};

4. Cyclic code arrangement

        (1) [Symmetrical generation] According to the characteristics of XOR, the final answer arrays are all XORed with the same number, and the result still meets the conditions, then take an x, so that, that is, we can use the method of start ^ x = 0question x = start ^ 03 Generate Gray code, and then multiply all elements of the array by one x.
        Time complexity: O ( n ) O(n)O ( n )

class Solution {
    
    
public:
    vector<int> circularPermutation(int n, int start) {
    
    
        vector<int> ans = {
    
    0, 1};
        while (-- n > 0) {
    
    
            int t = ans.size();
            for (int i = t - 1; i >= 0; -- i) {
    
    
                ans[i] <<= 1;
                ans.push_back(ans[i] + 1);
            }
        }
        int x = start ^ ans[0];
        for (auto& u: ans) {
    
    
            u ^= x;
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/mumuynsi/article/details/125214426