当前位置:网站首页>面试题 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
边栏推荐
- 5 minutes, turn your excel into an online database, the magic cube net table Excel database
- 王启亨谈Web3.0与价值互联网“通证交换”
- 捡起MATLAB的第(10)天
- Timing model: gated cyclic unit network (Gru)
- 【自娱自乐】构造笔记 week 2
- [split of recursive number] n points K, split of limited range
- MySQL - MySQL查询语句的执行过程
- Master vscode remote GDB debugging
- New developments: new trends in cooperation between smartmesh and meshbox
- Construction of esp32 compilation environment
猜你喜欢
【现代电子装联期末复习要点】
单体架构系统重新架构
王启亨谈Web3.0与价值互联网“通证交换”
Do we media make money now? After reading this article, you will understand
MySQL集群模式与应用场景
Read the meaning of serial port and various level signals
Spark 算子之coalesce与repartition
Coalesce and repartition of spark operators
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
CVPR 2022 quality paper sharing
随机推荐
vim指定行注释和解注释
Filter usage of spark operator
Go language slice, range, set
leetcode-374 猜数字大小
【自娱自乐】构造笔记 week 2
撿起MATLAB的第(9)天
GRBL学习(一)
leetcode-396 旋转函数
GRBL学习(二)
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
The length of the last word of the string
MySQL集群模式与应用场景
[section 5 if and for]
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
保姆级Anaconda安装教程
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
ESP32_ Arduino
Read the meaning of serial port and various level signals
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
MySQL optimistic lock to solve concurrency conflict