当前位置:网站首页>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;
}
};
边栏推荐
- KS009基于SSH实现宠物管理系统
- Upstream and downstream in software development
- What are the functions of soil microorganisms in microbial detection?
- Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
- 那些一门心思研究自动化测试的人,后来怎样了?
- Log4j2 ThreadContext日志链路追踪
- org. redisson. client. Redisresponsetimeoutexception: redis server response timeout (3000 ms) error resolution
- [fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother
- [Qt5 basic \u 1] starting from 0, Mr. Detian will study with you - Introduction to the window
- How does the property send a text message to the owner?
猜你喜欢

PHP crawls data through third-party plug-ins

【2022年】江西省研究生数学建模方案、代码

The whole process of AS400 API from zero to one

FL Studio20.9水果软件高级中文版电音编曲

New opportunities for vr/ar brought by metauniverse

Qt5 mvc: revealing the secrets of data visualization

After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up

How to maintain efficient collaboration in remote office and achieve stable growth of projects | community essay solicitation

AS400 API 从零到一的整个历程

亲测有效,快速创建JMeter桌面快捷方式
随机推荐
7-2 拼题A打卡奖励 dp
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
Electron pit Addon
股票开户有哪些优惠活动?另外,手机开户安全么?
哪有什么未来可期,不过是打工人临死前最后的幻想罢了
PHP crawls data through third-party plug-ins
P6773 [NOI2020] 命运(dp、线段树合并)
For the sustainable development of software testing, we must learn to knock code?
laravel 事件 & 订阅
Sun Yuchen told Swiss media Bilan that the bear market will not last long
Creating ASCII art with C #
Some essential differences
After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up
【JS给元素添加属性:setAttribute;classList.remove;classList.add;】
System. Csrebot for commandline
[Qt5 tab] tab label and content hierarchical analysis
[simulation] 922 Sort Array By Parity II
go导入自建包
[proteus simulation] Arduino UNO +74c922 keyboard decoding drive 4x4 matrix keyboard
Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey