当前位置:网站首页>Leetcode 面试题 17.10. 主要元素
Leetcode 面试题 17.10. 主要元素
2022-07-01 01:20:00 【我不是萧海哇~~~~】

数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组,找出其中的主要元素。若没有,返回 -1 。请设计时间复杂度为 O(N) 、空间复杂度为 O(1) 的解决方案。
示例 1:
输入:[1,2,5,9,5,9,5,5,5]
输出:5
示例 2:
输入:[3,2]
输出:-1
示例 3:
输入:[2,2,1,1,1,2,2]
输出:2
Code:
class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nums.end());
if(count(nums.begin(),nums.end(),nums[nums.size()/2])>nums.size()/2)
return nums[nums.size()/2];
return -1;
}
};
边栏推荐
- There is no future to be expected. It is just the last fantasy of a migrant worker before he dies
- 物业怎么发短信通知给业主?
- 【毕业季·进击的技术er】--毕业到工作小结
- [stack] 921 Minimum Add to Make Parentheses Valid
- [Qt5 basics] random number display
- Creating ASCII art with C #
- [simulation] 922 Sort Array By Parity II
- org.redisson.client.RedisResponseTimeoutException: Redis server response timeout (3000 ms)错误解决
- How does the property send a text message to the owner?
- 工作八年的程序员,却拿着毕业三年的工资,再不开窍就真晚了...
猜你喜欢

Log logrus third party library usage

小程序中实现excel数据的批量导入

The personal test is effective, and the JMeter desktop shortcut is quickly created
![[fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother](/img/fa/f9bad44147ba9af21183b7bd630e32.png)
[fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother

Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again

Connectivity basis of Graphs

删除重复的电子邮箱

数学知识:求组合数 III—求组合数

gin_ gorm

Unknown database connection database error
随机推荐
45 year old programmer tells you: why do programmers want to change jobs? It's too true
[queue] 933 Number of Recent Calls
What are the applications of SMS in enterprises?
The whole process of AS400 API from zero to one
[Office PDF] PDF merging and splitting will free us from the functional limitations of paid software, OK
gin_ gorm
数学知识:求组合数 IV—求组合数
QT web development - VIDEO - Notes
Some essential differences
[无线通信基础-15]:图解移动通信技术与应用发展-3- 数字通信2G GSM、CDMA、3G WDCMA/CDMA200/TD-SCDMA、4G LTE、5G NR概述
For the sustainable development of software testing, we must learn to knock code?
TypeError: Argument ‘angle‘ can not be treated as a double
There is no future to be expected. It is just the last fantasy of a migrant worker before he dies
一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!
【JS给元素添加属性:setAttribute;classList.remove;classList.add;】
Electron pit Addon
物业怎么发短信通知给业主?
[fundamentals of wireless communication-15]: illustrated mobile communication technology and application development-3-overview of digital communication 2G GSM, CDMA, 3G wdcma/cdma200/td-scdma, 4G LTE
【Content-Type请求头的内容】
如何学习和阅读代码