Java very simple solution


  • 0
    M

    Transforming the int to a binary representation in String and flipping the bits manually. Runtime: 12 ms

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

Log in to reply
 

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