My Solution with string


  • 0
    Y
    public static int findComplement(int num) {
         String s = Integer.toBinaryString(num);
           System.out.println(s);
           s = s.replaceAll("0","2");
           System.out.println(s);
           s= s.replaceAll("1","0");
           System.out.println(s);
           s= s.replaceAll("2","1");
    System.out.println(s);
           int numr = Integer.valueOf(s,2);
         return numr;
       } 
    

Log in to reply
 

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