题目描述

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

思路

和那道字符串里面第一次出现唯一字符的题目类似,使用count计数方法;另外百度了一下发现还可以用collections模块的Counter方法,把列表值和对应的个数组成一个字典

方法一:

class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
targets = []
for num in array:
if array.count(num)==1 and num not in targets:
targets.append(num)
return targets

方法二:

import collections
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
targets = []
dic = collections.Counter(array)
for key,value in dic.items():
if value < 2:
targets.append(key)
return targets

最新文章

  1. linux下普通用户如何使用80端口启动程序
  2. noi 04:求整数的和与均值
  3. 为了解决mysqlbing翻译表字段问题而分析frm文件(持续更新)
  4. css position属性
  5. DIV+CSS圆角边框
  6. .NET 请求被挂起,前端轮询,委托
  7. [转]第一章 Windows Shell是什么 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987862】
  8. RSA体系 c++/java相互进行加签验签--转
  9. mysql基础入门
  10. 关于Websockets问题:
  11. java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
  12. JAVA_SE基础——71.Random类制作随机验证码
  13. Summary on deep learning framework --- PyTorch
  14. dom4j 解析XML文件
  15. ubuntu 15 安装docker
  16. 《mysql必知必会》学习_第16章_20180807_欢
  17. git命令之git clone用法
  18. [转载]Oracle 游标使用全解
  19. python---django中序列化
  20. SQL NULL

热门文章

  1. Linear SVM和LR的区别和联系
  2. Oracle 11g透明网关连接Sqlserver
  3. 【Zend Studio】在Zend Studio中调试ThinkPhp框架
  4. 【基础篇】elasticsearch之索引模板Template[转]
  5. JS 的加密库简介
  6. [C++]深入解析结构化异常处理(SEH)
  7. Android Studio打包程序时出现transformClassesWithDexForRelease错误
  8. Java多线程:多线程基础知识
  9. [转]理解Go语言中的nil
  10. Vue:在vue-cli中使用Bootstrap