当前位置:网站首页>面试题 17.10. 主要元素
面试题 17.10. 主要元素
2022-04-23 15:58:00 【张.Joshua】
面试题 17.10. 主要元素
- 题号:力扣面试题 17.10
- 知识点:数组,计数
- 目标完成度:24/150
- 总结
题干:
思路:
- 1.本题难点在于时间复杂度为O(N),空间复杂度为O(1)的要求。使用摩尔投票法
- 2.摩尔投票法:
class Solution:
def majorityElement(self, nums: List[int]) -> int:
count = 0
ans = 0
for num in nums:
if count==0:
ans = num
if ans == num:
count += 1
if ans != num:
count -= 1
if nums.count(ans) > len(nums)//2:
return ans
else:
return -1
版权声明
本文为[张.Joshua]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44742084/article/details/124362770
边栏推荐
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- Intersection, union and difference sets of spark operators
- shell_ two
- MySQL - execution process of MySQL query statement
- 捡起MATLAB的第(3)天
- [key points of final review of modern electronic assembly]
- Matplotlib tutorial 05 --- operating images
- JVM - Chapter 2 - class loader subsystem
- C language --- advanced pointer
- Modèle de Cluster MySQL et scénario d'application
猜你喜欢
Single architecture system re architecture
Sortby use of spark operator
Spark 算子之coalesce与repartition
Nanny Anaconda installation tutorial
Day (8) of picking up matlab
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
volatile的含义以及用法
糖尿病眼底病变综述概要记录
Meaning and usage of volatile
Pgpool II 4.3 Chinese Manual - introductory tutorial
随机推荐
How important is the operation and maintenance process? I heard it can save 2 million a year?
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
MySQL - MySQL查询语句的执行过程
王启亨谈Web3.0与价值互联网“通证交换”
IronPDF for . NET 2022.4.5455
JS regular determines whether the port path of the domain name or IP is correct
Spark 算子之sortBy使用
Treatment of idempotency
leetcode-374 猜数字大小
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
Go concurrency and channel
Upgrade MySQL 5.1 to 5.66
Interview questions of a blue team of Beijing Information Protection Network
Config组件学习笔记
【现代电子装联期末复习要点】
为啥禁用外键约束
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Redis master-slave replication process
Best practices of Apache APIs IX high availability configuration center based on tidb