当前位置:网站首页>Interview question 17.10 Main elements
Interview question 17.10 Main elements
2022-04-23 16:03:00 【Zhang Joshua】
Interview questions 17.10. Main elements
- Question no : Press the interview questions 17.10
- Knowledge point : Array , Count
- Goal completion :24/150
- summary
stem :

Ideas :
- 1. The difficulty of this problem is that the time complexity is O(N), The space complexity is O(1) The requirements of . Using Moore voting
- 2. Moore voting :

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
版权声明
本文为[Zhang Joshua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231558306163.html
边栏推荐
- pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
- JVM - Chapter 2 - class loader subsystem
- Day (7) of picking up matlab
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- Website pressure measurement tools Apache AB, webbench, Apache jemeter
- Merging of Shanzhai version [i]
- C language --- string + memory function
- Es common query, sorting and aggregation statements
- 捡起MATLAB的第(2)天
- 实现缺省页面
猜你喜欢

Treatment of idempotency

451. 根据字符出现频率排序

OAK-D树莓派点云项目【附详细代码】

API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)

C语言自编字符串处理函数——字符串分割、字符串填充等
![[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention](/img/bf/2b4914276ec1083df697383fec8f22.png)
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention

New developments: new trends in cooperation between smartmesh and meshbox

R语言中实现作图对象排列的函数总结

Groupby use of spark operator

一文掌握vscode远程gdb调试
随机推荐
撿起MATLAB的第(9)天
Extract non duplicate integers
[key points of final review of modern electronic assembly]
如何进行应用安全测试(AST)
Unity Shader学习
js正則判斷域名或者IP的端口路徑是否正確
Fastjon2他来了,性能显著提升,还能再战十年
Grbl learning (I)
下载并安装MongoDB
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
Meaning and usage of volatile
Passing header request header information between services through feign
Sortby use of spark operator
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
Basic greedy summary
Spark 算子之distinct使用
Pgpool II 4.3 Chinese Manual - introductory tutorial
ES常用查询、排序、聚合语句
PS add texture to picture
Temporal model: long-term and short-term memory network (LSTM)