Number Complement

Problem

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

Solution

public class Solution {
    public int findComplement(int num) {
        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            sb.append(num % 2 == 0 ? "1" : "0");
            num /= 2;
        }
        return Integer.parseInt(sb.reverse().toString(), 2);
    }
}

Analysis

This know that bit value in last position is 1 if number is odd, 0 if number is even
Therefore, we can construct the complemented number in reverse order
We append 1 or 0 depending on the value is odd or even in opposite way we mentioned above
At the end, we use Integer.parseInt(string, 2) to convert a decimal number to binary number

results matching ""

    No results matching ""