Easy to understand Java solution


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

Log in to reply
 

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