当前位置:网站首页>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
边栏推荐
- Leetcode-374 guess the size of the number
- Spark 算子之partitionBy
- Timing model: gated cyclic unit network (Gru)
- 【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
- WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
- 面试题 17.10. 主要元素
- [split of recursive number] n points K, split of limited range
- Configuration of multi spanning tree MSTP
- Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
- homwbrew安装、常用命令以及安装路径
猜你喜欢
撿起MATLAB的第(9)天
C语言自编字符串处理函数——字符串分割、字符串填充等
The principle and common methods of multithreading and the difference between thread and runnable
一文读懂串口及各种电平信号含义
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
C language self compiled string processing function - string segmentation, string filling, etc
捡起MATLAB的第(9)天
实现缺省页面
Read the meaning of serial port and various level signals
Using JSON server to create server requests locally
随机推荐
ESP32编译环境的搭建
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
The principle and common methods of multithreading and the difference between thread and runnable
Accumulation of applet knowledge points
VIM specifies the line comment and reconciliation comment
C语言自编字符串处理函数——字符串分割、字符串填充等
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
Spark 算子之groupBy使用
多线程原理和常用方法以及Thread和Runnable的区别
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
Construction of esp32 compilation environment
捡起MATLAB的第(8)天
Day (8) of picking up matlab
Treatment of idempotency
Distinct use of spark operator
Ice -- source code analysis
Spark 算子之交集、并集、差集
Config组件学习笔记