题目
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请在时间复杂度为O(n),空间复杂度为O(1)的情况下,找出这两个数字。
Python题解
def find_first_bit_1(num):
index_1 = 0
while num & 1 == 0:
num = num >> 1
index_1 += 1
return index_1
def is_bit_1(num, index_bit):
num = num >> index_bit
return num & 1 == 1
def number_apperance_once(data):
if data is None or len(data) == 0:
return None, None
resXR = 0
for n in data:
resXR ^= n
index_of_1 = find_first_bit_1(resXR)
res_1, res_2 = 0, 0
for idx, num in enumerate(data):
if is_bit_1(num, index_of_1):
res_1 ^= num
else:
res_2 ^= num
return res_1, res_2