Java solution without using any library function


  • 0
    M
    
        public int findComplement(int num) {
            if(num==0) return 1;
            int res = 0;
            for(int i=0; i<31 && num!=0; i++){
                res += (((num & 1) ^ 1)<<i);
                num = num>>1;
            }
            return res;
        }
    
    

Log in to reply
 

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