Short C++ Solution


  • 0
    J
    class Solution {
    public:
        int findComplement(int num) {
            
            int n = num;
            while ((n & (n - 1)) != 0) 
                n &= n - 1;
                
            int mask = (n << 1) - 1;
            return mask ^ num;
        }
    };
    

Log in to reply
 

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