Java solution without bit manuplication


  • 0
    J
    public class Solution {
        public int findComplement(int num) {
            String s = Integer.toBinaryString(num).replaceAll("^0*", "");
            if (s == "") s = "0";
            
            StringBuffer sb = new StringBuffer();
            for (char c: s.toCharArray()) {
                sb.append(c == '0' ? '1' : '0');
            }
            
            return Integer.parseInt(sb.toString(), 2);
        }
    }
    

Log in to reply
 

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