当前位置:网站首页>面试题 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
边栏推荐
猜你喜欢

实现缺省页面

保姆级Anaconda安装教程

Vision of building interstellar computing network

贫困的无网地区怎么有钱建设网络?

【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope

Day (8) of picking up matlab

MySQL - execution process of MySQL query statement

How important is the operation and maintenance process? I heard it can save 2 million a year?

Grbl learning (I)

撿起MATLAB的第(9)天
随机推荐
贫困的无网地区怎么有钱建设网络?
保姆级Anaconda安装教程
捡起MATLAB的第(6)天
[self entertainment] construction notes week 2
Neodynamic Barcode Professional for WPF V11.0
IronPDF for . NET 2022.4.5455
Do we media make money now? After reading this article, you will understand
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
Day (7) of picking up matlab
捡起MATLAB的第(8)天
CVPR 2022 quality paper sharing
ES常用查询、排序、聚合语句
【自娱自乐】构造笔记 week 2
Day (10) of picking up matlab
MySQL - execution process of MySQL query statement
捡起MATLAB的第(3)天
MySQL optimistic lock to solve concurrency conflict
TIA博图——基本操作
Read the meaning of serial port and various level signals
Spark 算子之sortBy使用