题目描述:
一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
python代码:
方法1 使用字典记录数组中的值及对应值出现的次数,然后搜索出现一次的数字。
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
dict1 = {}
for i in array:
if i in dict1:
dict1[i] += 1
else:
dict1[i] = 1
res = []
for k in dict1:
if dict1[k] == 1:
res.append(k)
return res
方法2 使用python中的count计数函数。
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
res = []
for i in array:
if array.count(i)==1 and i not in res:
res.append(i)
return res