当前位置:网站首页>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;
}
};
边栏推荐
- (翻译)实时内联验证更容易让用户犯错的原因
- The whole process of AS400 API from zero to one
- Gin configuration file
- 【JS给元素添加属性:setAttribute;classList.remove;classList.add;】
- 测试必备工具—Postman实战教程
- 软件开发中的上游和下游
- Handsontable数据网格组件
- Analysis on user behavior loss of data exploration e-commerce platform
- The personal test is effective, and the JMeter desktop shortcut is quickly created
- Laravel+redis generates an order number - automatically increase from 1 on the same day
猜你喜欢

医疗HIS行业短信发送解决方案

Neo4j installation, operation, project construction and function realization
![[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大](/img/fa/f9bad44147ba9af21183b7bd630e32.png)
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大

What will Web3 bring in the future?

Microbiological health, why is food microbiological testing important

7-2 punch in reward DP for puzzle a

Institute of Microbiology, commonly used biochemical reactions in microbiological testing

Necessary tools for testing - postman practical tutorial

Selenium classic interview question - multi window switching solution

Selenium经典面试题-多窗口切换解决方案
随机推荐
聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
int和位数组互转
计算特殊奖金
Batch import of Excel data in applet
短视频平台开发,依靠DrawerLayout实现侧滑菜单效果
关于白盒测试,这些技巧你得游刃有余~
How does ZABBIX configure alarm SMS? (alert SMS notification setting process)
45 year old programmer tells you: why do programmers want to change jobs? It's too true
(翻译)使用眉状文本提高标题点击率
医疗HIS行业短信发送解决方案
php将二维数组元素转为键值对
微生物安全與健康,什麼是生物處理?
[queue] 933 Number of Recent Calls
With regard to the white box test, you have to master these skills~
opencv -- 笔记
【Proteus仿真】Arduino UNO +74C922键盘解码驱动4X4矩阵键盘
Sécurité et santé microbiennes, qu'est - ce que le traitement biologique?
Selenium classic interview question - multi window switching solution
Laravel+redis generates an order number - automatically increase from 1 on the same day
AS400 entretien d'usine