Java Solution


  • 0
    L
        public int findComplement(int num) {
            if (num == 1) {
                return 0;
            }
            
            String b = Integer.toBinaryString(num);
            String c = "";
            for (int i = 0; i < b.length(); i++) {
                if (b.charAt(i) == '0') {
                    c += "1";
                } else {
                    c += "0";
                }
            }
            return Integer.parseInt(c, 2);
        }
    

Log in to reply
 

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