当前位置:网站首页>Leetcode interview question 17.10 Main elements
Leetcode interview question 17.10 Main elements
2022-07-01 02:09:00 【I'm not xiaohaiwa~~~~】

More than half of the elements in an array are called major elements . To give you one Integers Array , Find out the main elements . If there is no , return -1 . Please design the time complexity as O(N) 、 The space complexity is O(1) Solutions for .
Example 1:
Input :[1,2,5,9,5,9,5,5,5]
Output :5
Example 2:
Input :[3,2]
Output :-1
Example 3:
Input :[2,2,1,1,1,2,2]
Output :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;
}
};
边栏推荐
- 【做题打卡】集成每日5题分享(第一期)
- [punch in questions] integrated daily 5 questions sharing (phase I)
- Qu'est - ce que le PMP?
- For the sustainable development of software testing, we must learn to knock code?
- 数学知识:求组合数 III—求组合数
- Leetcode(524)——通过删除字母匹配到字典里最长单词
- 如何选择券商?另外,手机开户安全么?
- Some uses of Halcon array
- Handsontable data grid component
- Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
猜你喜欢

Machine learning 10 belief Bayesian classifier
![[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

(翻译)使用眉状文本提高标题点击率

The whole process of AS400 API from zero to one

Fast understanding of forward proxy and reverse proxy

Selenium经典面试题-多窗口切换解决方案

Fix names in the table (first character uppercase, other lowercase)

CorelDRAW 2022中文精简64位直装版下载

機器學習10-信念貝葉斯分類器

正向代理和反向代理快速理解
随机推荐
Necessary tools for testing - postman practical tutorial
SWT / anr problem - binder stuck
P6773 [NOI2020] 命运(dp、线段树合并)
How does the property send a text message to the owner?
With regard to the white box test, you have to master these skills~
SWT/ANR问题--AMS/WMS
VirtualBox 安装增强功能
electron之坑addon
Clickhouse 消除由group by产生的间隙
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
SWT/ANR问题--Native方法执行时间过长导致SWT
In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!
Leetcode (524) -- match the longest word in the dictionary by deleting letters
Electron pit Addon
P6773 [noi2020] destiny (DP, segment tree merging)
【JS】【掘金】获取关注了里不在关注者里的人
修复表中的名字(首字符大写,其他小写)
零基础自学SQL课程 | 窗口函数
halcon变量窗口的图像变量不显示,重启软件和电脑都没用
My PMP learning test experience