Some bitwise-operation trick


  • 0
    C
    class Solution {
    public:
        int findComplement(int num) {
            unsigned mask = num;
            mask |= mask >> 1;
            mask |= mask >> 2;
            mask |= mask >> 4;
            mask |= mask >> 8;
            mask |= mask >> 16;
            
            return mask & ~num;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.