本题注意点:二/十进制互转、异或位运算实现0、1互转
import numpy as np
def findComplement(num):
"""
:type num: int
:rtype: int
"""
t = bin(num)[2:]
s = ''
for i in range(len(t)):
if t[i] == '1':
s += '0'
else:
s += '1'
return int(s, 2)
# version2:
# temp = num
# n = 0
# while temp > 0:
# n += 1
# temp = temp >> 1
# return num ^ (2 ** n - 1)
# sample:
num = 5
print(findComplement(num))