当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

Fast understanding of forward proxy and reverse proxy
![Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]

With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!

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

Batch import of Excel data in applet

PMP是什么?

(summary I) Halcon Foundation's target finding features + becoming a regular

How does the property send a text message to the owner?

机器学习9-通用逼近器径向基函数神经网络,在新观点下审视PDA和SVM

(translation) use eyebrow shaped text to improve Title click through rate
随机推荐
思特奇加入openGauss开源社区,共同推动数据库产业生态发展
Laravel event & subscription
VirtualBox 安装增强功能
What are the preferential activities for stock account opening? In addition, is it safe to open a mobile account?
MySQL insert \ pre update + judgment condition
【JS】【掘金】获取关注了里不在关注者里的人
@ConfigurationProperties和@Value的区别
数学知识:求组合数 IV—求组合数
对象与对象变量
QT web 开发 - video -- 笔记
How to learn and read code
Alphabet-Rearrange-Inator 3000(字典树自定义排序)
先写API文档还是先写代码?
House change for agricultural products? "Disguised" house purchase subsidy!
Test essential tool - postman practical tutorial
Winodws 快速添加开机启动项
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
(翻译)使用眉状文本提高标题点击率
AS400 large factory interview
Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation