当前位置:网站首页>229. 求众数 II
229. 求众数 II
2022-04-23 04:15:00 【张.Joshua】
229. 求众数 II
- 题号:力扣229
- 知识点:计数,哈希,数组
- 目标完成度:21/150
- 总结
题干:
思路:
- 1.通过字典构造散列表,
cnt={key:value, ...}
,以数组中的值为字典的key,以数组中的值出现的次数为字典的value - 2.遍历数组,将数组中的元素和元素出现的次数存储在字典
cnt
中 - 3.查找字典中
value
值大于n//3
的key
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
cnt = {
}
ans = []
for v in nums:
if v in cnt:
cnt[v] += 1
else:
cnt[v] = 1
for key in cnt.keys():
if cnt[key] > (len(nums) // 3):
ans.append(key)
return ans
版权声明
本文为[张.Joshua]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44742084/article/details/124355761
边栏推荐
- HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
- Writing latex with vscode - the latest tutorial 2022 / 4 / 17
- 作为一名码农,女友比自己更能码是一种什么体验?
- As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
- 优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
- Nature medicine reveals individual risk factors of coronary artery disease
- ERROR: Could not find a version that satisfies the requirement win32gui
- Basic introduction to spot gold
- The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
- Summary of knowledge map (3)
猜你喜欢
STM32上μC/Shell移植与应用
Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
Matlab minimalist configuration of vscode configuration
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
Express中间件②(中间件的分类)
[echart] Introduction to echart
STM32单片机ADC规则组多通道转换-DMA模式
Shopping mall for transportation tools based on PHP
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
随机推荐
【测绘程序设计】坐标方位角推算神器(C#版)
Qtspim manual - Chinese Translation
C语言:恶搞小游戏
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
The latest price trend chart and trading points of London Silver
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
Basic introduction to spot gold
现货黄金操作技巧_估波曲线
【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
智能电子秤全国产化电子元件推荐方案
单片机串口数据处理(1)——串口中断发送数据
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
顺序表的基本操作
无线键盘全国产化电子元件推荐方案
伦敦银最新价格走势图与买卖点
C language character constant
[string] ranking of country names ----- problem solving notes
TreeSet after class exercises