当前位置:网站首页>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
边栏推荐
- MySQL - MySQL查询语句的执行过程
- MySQL - execution process of MySQL query statement
- Basic greedy summary
- Win11/10家庭版禁用Edge的inprivate浏览功能
- Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
- R语言中绘制ROC曲线方法二:pROC包
- 面试题 17.10. 主要元素
- Merging of Shanzhai version [i]
- Day (8) of picking up matlab
- Method 2 of drawing ROC curve in R language: proc package
猜你喜欢

安装Redis并部署Redis高可用集群

Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)

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

一文读懂串口及各种电平信号含义

Homewbrew installation, common commands and installation path

Install redis and deploy redis high availability cluster

Large factory technology implementation | industry solution series tutorials

Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago

捡起MATLAB的第(6)天

C language self compiled string processing function - string segmentation, string filling, etc
随机推荐
Config learning notes component
Day (5) of picking up matlab
Day (10) of picking up matlab
New developments: new trends in cooperation between smartmesh and meshbox
Intersection, union and difference sets of spark operators
运维流程有多重要,听说一年能省下200万?
Vim使用Vundle安装代码补全插件(YouCompleteMe)
捡起MATLAB的第(9)天
Homewbrew installation, common commands and installation path
保姆级Anaconda安装教程
ESP32编译环境的搭建
JS regular determines whether the port path of the domain name or IP is correct
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
volatile的含义以及用法
C语言自编字符串处理函数——字符串分割、字符串填充等
Simple usage of dlopen / dlsym / dlclose
Leetcode-396 rotation function
VIM specifies the line comment and reconciliation comment
Coalesce and repartition of spark operators
捡起MATLAB的第(6)天