当前位置:网站首页>面试题 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
边栏推荐
- Construction of esp32 compilation environment
- Named in pytoch_ parameters、named_ children、named_ Modules function
- CVPR 2022 quality paper sharing
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- Pgpool II 4.3 Chinese Manual - introductory tutorial
- Large factory technology implementation | industry solution series tutorials
- Application case of GPS Beidou high precision satellite time synchronization system
- [self entertainment] construction notes week 2
- Leetcode-396 rotation function
- 一文掌握vscode远程gdb调试
猜你喜欢
新动态:SmartMesh和MeshBox的合作新动向
Method 2 of drawing ROC curve in R language: proc package
捡起MATLAB的第(4)天
Partitionby of spark operator
Construction of esp32 compilation environment
Intersection, union and difference sets of spark operators
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
大型互联网为什么禁止ip直连
贫困的无网地区怎么有钱建设网络?
Multi level cache usage
随机推荐
[self entertainment] construction notes week 2
Modèle de Cluster MySQL et scénario d'application
Groupby use of spark operator
Compile, connect -- Notes
ESP32_ Arduino
homwbrew安装、常用命令以及安装路径
Interview questions of a blue team of Beijing Information Protection Network
捡起MATLAB的第(5)天
捡起MATLAB的第(4)天
Cap theorem
The principle and common methods of multithreading and the difference between thread and runnable
Spark 算子之distinct使用
Day (10) of picking up matlab
Coalesce and repartition of spark operators
matplotlib教程05---操作图像
Config组件学习笔记
Spark 算子之partitionBy
leetcode-374 猜数字大小
C language self compiled string processing function - string segmentation, string filling, etc
Merging of Shanzhai version [i]