题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
python实现:
# -*- coding:utf-8 -*-
from collections import Counter
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
c = Counter(array)
d = list(c.most_common()[-2:])
e = list(d[0])
f = list(d[1])
return e[0],f[0]
调用了Counter这个类,具体可以参考https://blog.csdn.net/u013628152/article/details/43198605这篇博客的用法
法二:
a = []
c = 0
for i in range(0, len(array)):
flag = 0
for j in range(0, len(array)):
if (array[i] == array[j]) and (i != j):
flag = 1
break
if flag == 0:
a.append(array[i])
if len(a) == 2:
break
return a[0], a[1]