Easy Java solution without using bit manipulation


  • 0
    N
        public int findComplement(int num) {
           
            String s = Integer.toBinaryString(num);
            int result =0,j=s.length()-1;
            for(int i=0;i<s.length();i++){
             if(s.charAt(i)=='0')  result+=Math.pow(2,j);
             j--;
            }
            return result;
        }
    }

Log in to reply
 

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