第一种:找出成对数值并取出,剩下就为出现一次的。
class Solution:
def FindNumsAppearOnce(self,array):
if not array:
return []
res=[]
count=0
while len(array)>0:
p=array.pop()
if p in array:
array.remove(p)
else:
res.append(p)
return res
第二种统计出现次数
class Solution:
def FindNumsAppearOnce(self,array):
hashMap={}
for i in array:
if str(i) in hashMap:
hashMap[str(i)]+=1
else:
hashMap[str(i)]=1
res=[]
for k in hashMap.keys():
if hashMap[k]==1:
res.append(int(k))
return res
第三种异或法
class Solution:
def FindNumsAppearOnce(self, array):
if not array:
return []
# 对array中的数字进行异或运算
tmp = 0
for i in array:
tmp ^= i
# 获取tmp中最低位1的位置
idx = 0
while (tmp & 1) == 0:
tmp >>= 1
idx += 1
a = b = 0
for i in array:
if self.isBit(i, idx):
a ^= i
else:
b ^= i
return [a, b]
def isBit(self, num, idx):
"""
判断num的二进制从低到高idx位是不是1
:param num: 数字
:param idx: 二进制从低到高位置
:return: num的idx位是否为1
"""
num = num >> idx
return num & 1