题目描述

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
方法1:

  1. # -*- coding:utf-8 -*-
  2. class Solution:
  3. # 返回[a,b] 其中ab是出现一次的两个数字
  4. def FindNumsAppearOnce(self, array):
  5. # write code here
  6. if len(array)==0:
  7. return 0
  8. count_dict = {}
  9. result = []
  10. for i in array:
  11. count_dict[i]=count_dict.get(i,0)+1
  12. for key,value in count_dict.items():
  13. if value==1:
  14. result.append(key)
  15. return result

方法2:

  1. class Solution:
  2. # 返回[a,b] 其中ab是出现一次的两个数字
  3. def FindNumsAppearOnce(self, array):
  4. # write code here
  5. if len(array)==0:
  6. return 0
  7. result = []
  8. for i in array:
  9. if array.count(i)==1:
  10. result.append(i)
  11. return result