数组中只出现过一次的数字 牛客网 剑指Offer
数组中只出现过一次的数字 牛客网 剑指Offer
- 题目描述
- 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
def FindNumsAppearOnce(self, array):
if array == None or len(array) <= 0:
return []
resultExOr = self.ExOr(array)
i = 0
while resultExOr and i <= 32:
i += 1
resultExOr = resultExOr>>1
num1, num2 = [], []
for num in array:
if self.bitIs1(num, i):
num1.append(num)
else:
num2.append(num)
first = self.ExOr(num1)
second = self.ExOr(num2)
return [first, second]
def ExOr(self, aList):
ExOrNum = 0
for i in aList:
ExOrNum = ExOrNum ^ i
return ExOrNum
def bitIs1(self, n, i):
n = n >> (i-1)
return n & 1