当前位置:网站首页>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
边栏推荐
- Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
- One brush 314 sword finger offer 09 Implement queue (E) with two stacks
- GRBL学习(二)
- API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
- Day (6) of picking up matlab
- Unity shader learning
- 如何进行应用安全测试(AST)
- 451. 根据字符出现频率排序
- VIM specifies the line comment and reconciliation comment
- Config learning notes component
猜你喜欢
Groupby use of spark operator
homwbrew安装、常用命令以及安装路径
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
面试题 17.10. 主要元素
保姆级Anaconda安装教程
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
C language --- advanced pointer
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
随机推荐
Partitionby of spark operator
Leetcode-396 rotation function
捡起MATLAB的第(2)天
Es common query, sorting and aggregation statements
Spark 算子之coalesce与repartition
C, calculation method and source program of bell number
js正则判断域名或者IP的端口路径是否正确
Distinct use of spark operator
Day (8) of picking up matlab
Day (4) of picking up matlab
Deletes the least frequently occurring character in the string
Grbl learning (II)
String sorting
Application case of GPS Beidou high precision satellite time synchronization system
C#,贝尔数(Bell Number)的计算方法与源程序
Master vscode remote GDB debugging
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
The principle and common methods of multithreading and the difference between thread and runnable
ESP32编译环境的搭建
Intersection, union and difference sets of spark operators