题目内容
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
题目思路
除了直接一个一个的求之外,我们可以考虑遍历整个数组,并且建立辅助数组来求解。如果这个数字已经在辅助数组中,那么跳过。如果不在但是后面还有,那就把这个数加入辅助数组之中。否则就加入结果中,如果已经找到两个则返回。
程序代码
# -*- coding:utf-8 -*-
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
note=[]
res=[]
for i in range(len(array)):
if array[i] in note:
continue
elif array[i] in array[i+1:]:
note.append(array[i])
else:
res.append(array[i])
if len(res)==2:
return res