当前位置:网站首页>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
边栏推荐
- Unity shader learning
- Go language, condition, loop, function
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- JS regular determines whether the port path of the domain name or IP is correct
- PS为图片添加纹理
- Leetcode-396 rotation function
- linux上启动oracle服务
- 一文读懂串口及各种电平信号含义
- 贫困的无网地区怎么有钱建设网络?
- Interview questions of a blue team of Beijing Information Protection Network
猜你喜欢
How can poor areas without networks have money to build networks?
新动态:SmartMesh和MeshBox的合作新动向
保姆级Anaconda安装教程
MySQL - execution process of MySQL query statement
ESP32编译环境的搭建
The principle and common methods of multithreading and the difference between thread and runnable
捡起MATLAB的第(7)天
Method 2 of drawing ROC curve in R language: proc package
Config learning notes component
Filter usage of spark operator
随机推荐
JVM - Chapter 2 - class loader subsystem
Large factory technology implementation | industry solution series tutorials
How do you think the fund is REITs? Is it safe to buy the fund through the bank
Algorithem_ ReverseLinkedList
安装Redis并部署Redis高可用集群
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
JS regular détermine si le nom de domaine ou le chemin de port IP est correct
捡起MATLAB的第(4)天
C language self compiled string processing function - string segmentation, string filling, etc
PS为图片添加纹理
[self entertainment] construction notes week 2
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
JS regular determines whether the port path of the domain name or IP is correct
C语言自编字符串处理函数——字符串分割、字符串填充等
Matplotlib tutorial 05 --- operating images
Nanny Anaconda installation tutorial
GRBL学习(一)
Day (10) of picking up matlab
volatile的含义以及用法
Win11 / 10 home edition disables the edge's private browsing function