当前位置:网站首页>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;
}
};
边栏推荐
- Mathematical knowledge: 01 sequence satisfying conditions - find combinatorial number
- SWT/ANR问题--Dump时间过长导致的SWT
- What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
- What is the difference between port number and process number?
- 开源基础软件公司,寻找一起创造未来的你(API7.ai)
- What is PMP?
- CorelDRAW 2022 Chinese Simplified 64 bit direct download
- Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
- Short video platform development, relying on drawerlayout to achieve side sliding menu effect
- (总结一)Halcon基础之寻找目标特征+转正
猜你喜欢
FL Studio20.9水果软件高级中文版电音编曲
SWT / anr problem - binder stuck
数据探索电商平台用户行为流失分析
House change for agricultural products? "Disguised" house purchase subsidy!
int和位数组互转
electron之坑addon
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
AS400 API 从零到一的整个历程
CorelDRAW 2022中文精简64位直装版下载
CorelDRAW 2022 Chinese Simplified 64 bit direct download
随机推荐
How does ZABBIX configure alarm SMS? (alert SMS notification setting process)
Opencv -- Notes
【JS】【掘金】获取关注了里不在关注者里的人
The whole process of AS400 API from zero to one
Template: globally balanced binary tree
@The difference between configurationproperties and @value
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
機器學習10-信念貝葉斯分類器
SWT / anr problem - SWT caused by too long dump time
(翻译)实时内联验证更容易让用户犯错的原因
Clickhouse 消除由group by产生的间隙
P6773 [noi2020] destiny (DP, segment tree merging)
With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!
Log4j2 ThreadContext日志链路追踪
Some uses of Halcon array
Use of laravel carbon time processing class
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
What is the difference between port number and process number?
How does the property send a text message to the owner?
There is no future to be expected. It is just the last fantasy of a migrant worker before he dies