Easy C/C++ Solution


  • 0
    class Solution {
    public:
        int findComplement(int num) {
            int i=0,num2,num3;
            num2=num;
            while(num!=0){
                num=num>>1;                                         
                num3=pow(2,i);                      //XOR each bit  with 1
                num2=num2^num3;
                i++;
            }
            return num2;
        }
    };
    

Log in to reply
 

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