剑指offer——python【第40题】数组中只出现一次的数字
2024-08-24 20:00:28
题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
思路
和那道字符串里面第一次出现唯一字符的题目类似,使用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
最新文章
- linux下普通用户如何使用80端口启动程序
- noi 04:求整数的和与均值
- 为了解决mysqlbing翻译表字段问题而分析frm文件(持续更新)
- css position属性
- DIV+CSS圆角边框
- .NET 请求被挂起,前端轮询,委托
- [转]第一章 Windows Shell是什么 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987862】
- RSA体系 c++/java相互进行加签验签--转
- mysql基础入门
- 关于Websockets问题:
- java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
- JAVA_SE基础——71.Random类制作随机验证码
- Summary on deep learning framework --- PyTorch
- dom4j 解析XML文件
- ubuntu 15 安装docker
- 《mysql必知必会》学习_第16章_20180807_欢
- git命令之git clone用法
- [转载]Oracle 游标使用全解
- python---django中序列化
- SQL NULL
热门文章
- Linear SVM和LR的区别和联系
- Oracle 11g透明网关连接Sqlserver
- 【Zend Studio】在Zend Studio中调试ThinkPhp框架
- 【基础篇】elasticsearch之索引模板Template[转]
- JS 的加密库简介
- [C++]深入解析结构化异常处理(SEH)
- Android Studio打包程序时出现transformClassesWithDexForRelease错误
- Java多线程:多线程基础知识
- [转]理解Go语言中的nil
- Vue:在vue-cli中使用Bootstrap