Simple recursive solution in C++ (3 lines of code)


  • 0
    G
    class Solution {
    public:
    
        int findComplement(int num) 
        {
            if(num < 2)
            {
                return (1 - num);
            }
            
            return ((findComplement(num >> 1) << 1) | (1 - (num & 1)));
        }
    };

Log in to reply
 

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