Simple Java Solution


  • 0
    S
    public static int findComplement(int num) {
    		String binaryString = Integer.toBinaryString(num);
    		String temp = "";
    		for (char c : binaryString.toCharArray()) {
    			if (c == '1') {
    				temp += "0";
    			} else {
    				temp += "1";
    			}
    		}
    		return Integer.parseInt(temp, 2);
    	}
    

Log in to reply
 

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