Very easy and understandable solution( C# , also will work on C++/JAVA without changes)


  • 0
    A
    public int FindComplement(int num)
            {
                int mask = 1;
                int result = 0;
                while (mask < num)
                {
                    mask |= mask << 1;
                    result = num ^ mask;
                }
                return result;
            }
    

Log in to reply
 

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