当前位置:网站首页>面试题 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
边栏推荐
- Groupby use of spark operator
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- Meaning and usage of volatile
- gps北斗高精度卫星时间同步系统应用案例
- C语言自编字符串处理函数——字符串分割、字符串填充等
- shell_ two
- 5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
- js正则判断域名或者IP的端口路径是否正确
- 贫困的无网地区怎么有钱建设网络?
- Unity shader learning
猜你喜欢
随机推荐
Sortby use of spark operator
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
多线程原理和常用方法以及Thread和Runnable的区别
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
保姆级Anaconda安装教程
5 minutes, turn your excel into an online database, the magic cube net table Excel database
【自娱自乐】构造笔记 week 2
Single architecture system re architecture
vim指定行注释和解注释
leetcode-396 旋转函数
MySQL - execution process of MySQL query statement
Large factory technology implementation | industry solution series tutorials
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
TIA botu - basic operation
Neodynamic Barcode Professional for WPF V11. 0
C语言自编字符串处理函数——字符串分割、字符串填充等
Unity Shader学习
homwbrew安装、常用命令以及安装路径
通过Feign在服务之间传递header请求头信息
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)