Simple but different Java solution


  • 0
    L
    public class Solution {
        public int findComplement(int num) {
            String binary = Integer.toBinaryString(num);
            int sum=0;
            for(int i=0;i<binary.length();i++) {
                int val = 2*((binary.charAt(i)-'0')^1);
                if(val>0) 
                sum += Math.pow(val,binary.length()-1-i);
            }
            return sum;
        }
    }
    
    

Log in to reply
 

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