Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Example
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.
Solution
class Solution:
def findComplement(self, num: int) -> int:
temp = num
count = 1
while temp:
temp //= 2
count *= 2
count -= 1
return num^count