题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
解题思路
本题使用C++需要运用异或的思路,暂时没有写出来。暂时使用Python解题,使用了字典的技巧。
# -*- coding:utf-8 -*-
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
if len(array) < 2:
return None
dict, res = {}, []
for num in array:
dict[num] = 1 if num in dict else 0
for key in dict:
if dict[key] == 0:
res.append(key)
return res