当前位置:网站首页>面试题 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
边栏推荐
- 一文掌握vscode远程gdb调试
- GRBL学习(一)
- 5 minutes, turn your excel into an online database, the magic cube net table Excel database
- s16. One click installation of containerd script based on image warehouse
- Upgrade MySQL 5.1 to 5.66
- The principle and common methods of multithreading and the difference between thread and runnable
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- 新动态:SmartMesh和MeshBox的合作新动向
- [split of recursive number] n points K, split of limited range
- Redis master-slave replication process
猜你喜欢

CVPR 2022 quality paper sharing

New developments: new trends in cooperation between smartmesh and meshbox

MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问

MySQL集群模式與應用場景

Spark 算子之coalesce与repartition

homwbrew安装、常用命令以及安装路径

Neodynamic Barcode Professional for WPF V11. 0

Grbl learning (I)

Load Balancer

Config learning notes component
随机推荐
Implement default page
JS regular determines whether the port path of the domain name or IP is correct
【自娱自乐】构造笔记 week 2
Win11/10家庭版禁用Edge的inprivate浏览功能
Pgpool II 4.3 Chinese Manual - introductory tutorial
js正則判斷域名或者IP的端口路徑是否正確
贫困的无网地区怎么有钱建设网络?
IronPDF for .NET 2022.4.5455
Go language, array, pointer, structure
Codejock Suite Pro v20.3.0
Calculate the number of occurrences of a character
Filter usage of spark operator
Load Balancer
Multi level cache usage
Cap theorem
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Why disable foreign key constraints
String sorting
撿起MATLAB的第(9)天
One brush 313 sword finger offer 06 Print linked list from end to end (E)