当前位置:网站首页>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;
}
};
边栏推荐
- SWT/ANR问题--ANR/JE引发SWT
- 机器学习10-信念贝叶斯分类器
- Leetcode 面试题 17.10. 主要元素
- Machine learning 10 belief Bayesian classifier
- Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
- 如何选择券商?另外,手机开户安全么?
- (翻译)实时内联验证更容易让用户犯错的原因
- The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
- 2022年最新csdn涨薪技术栈-app自动化测试概述
- Is there any discount for opening an account now? In addition, is it safe to open a mobile account?
猜你喜欢

求两个线段公共部分的长度

@The difference between configurationproperties and @value

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

思特奇加入openGauss开源社区,共同推动数据库产业生态发展

Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)

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

Necessary tools for testing - postman practical tutorial

Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。

计算特殊奖金

(翻译)实时内联验证更容易让用户犯错的原因
随机推荐
计算特殊奖金
如何学习和阅读代码
Leetcode(524)——通过删除字母匹配到字典里最长单词
QML control type: tooltip
halcon数组的一些使用
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
Calculate special bonus
【JS】【掘金】获取关注了里不在关注者里的人
SWT / anr problem - anr/je causes SWT
How does ZABBIX configure alarm SMS? (alert SMS notification setting process)
Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
SWT/ANR问题--StorageManagerService卡住
运算符重载的初识
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
项目管理是什么?
机器学习10-信念贝叶斯分类器
Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]
对象与对象变量
Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation
数据探索电商平台用户行为流失分析