Java : using Integer.toBinaryString


  • 0
    H

    class Solution {
    public int findComplement(int num) {

        String binaryString = Integer.toBinaryString(num);
        
        String str="";
        
        for(int i=0;i<binaryString.length();i++)
        {
            str += binaryString.charAt(i)=='0'?'1':'0';
            
        }
        return Integer.parseInt(str,2);
    }
    

    }


Log in to reply
 

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