C# - 3 lines - O(highest set bit)


  • 2

    the complexity will be the number of digits up to the highest set bit.

        public int FindComplement(int num) 
        {
            int mask = 1;
            while (mask < num) mask = (mask << 1) | 1;
            return ~num & mask;
        }
    

Log in to reply
 

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