当前位置:网站首页>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;
}
};
边栏推荐
- 如何学习和阅读代码
- 农产品换房?“变相”购房补贴!
- What are the applications of SMS in enterprises?
- 直播商城源码,实现左右联动商品分类页面
- 如何选择券商?另外,手机开户安全么?
- Sun Yuchen told Swiss media Bilan that the bear market will not last long
- 短视频平台开发,依靠DrawerLayout实现侧滑菜单效果
- 求两个线段公共部分的长度
- KS009基于SSH实现宠物管理系统
- How to maintain efficient collaboration in remote office and achieve stable growth of projects | community essay solicitation
猜你喜欢

Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
2022年最新csdn涨薪技术栈-app自动化测试概述

7-2 拼题A打卡奖励 dp

New opportunities for vr/ar brought by metauniverse

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

AS400 API 从零到一的整个历程

The personal test is effective, and the JMeter desktop shortcut is quickly created

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

Creating ASCII art with C #

3500字归纳总结:一名合格的软件测试工程师需要掌握的技能大全
随机推荐
How to select securities companies? In addition, is it safe to open a mobile account?
Some items of OCR
[Qt5 basic \u 1] starting from 0, Mr. Detian will study with you - Introduction to the window
After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up
软件测试的可持续发展,必须要学会敲代码?
Try new possibilities
机器学习9-通用逼近器径向基函数神经网络,在新观点下审视PDA和SVM
Ks009 implementation of pet management system based on SSH
PHP通过第三方插件爬取数据
Microbiological health, why is food microbiological testing important
AS400 large factory interview
[simulation] 922 Sort Array By Parity II
Short message sending solution in medical his industry
[dynamic planning] path dp:931 Minimum Falling Path Sum
Test essential tool - postman practical tutorial
测试必备工具—Postman实战教程
物业怎么发短信通知给业主?
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
Winodws 快速添加开机启动项
laravel 事件 & 订阅