当前位置:网站首页>【Day_03 0420】数组中出现次数超过一半的数字
【Day_03 0420】数组中出现次数超过一半的数字
2022-07-26 06:08:00 【安河桥畔】
数组中出现次数超过一半的数字
题目来源
牛客网:数组中出现次数超过一半的数字
题目描述
给一个长度为 n 的数组,数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
例如输入一个长度为9的数组[1,2,3,2,2,2,5,4,2]。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
数据范围:n≤50000,数组中元素的值 0≤val≤10000
要求:空间复杂度:O(1),时间复杂度 O(n)
输入描述
输入的第一行为一个正整数n(1 ≤ n ≤ 10^5)
第二行包括3*n个整数a_i(1 ≤ a_i ≤ 10^9),表示每个参赛选手的水平值.
示例1
输入
[1,2,3,2,2,2,5,4,2]
返回值
2
示例2
输入
[3,3,3,3,2,2,2]
返回值
3
示例3
输入
[1]
返回值
1
思路分析
解法一
- 抵消法
- 如果两个数不相等,就消去这两个数,最坏情况下,每次消去一个众数和一个非众数,那么如果存在众数,最后留下的数肯定是众数
- 定义一个ret保存当前的众数,定义times表示其出现的次数
- 遍历数组,当前数组值与ret相等则增加times值,不相等则减小times值,times值为0时用当前数组元素的值替换ret
解法二
- 对数组进行排序
- 找数组中间的数,因为一个数如果在数组中存在次数超过一半,则排序后数组中间的数一定是这个数
代码展示
解法一
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int times = 1;
int ret = numbers[0];//定义ret为数组第一个元素,如果数组只有一个元素,可以直接返回ret
int sz = numbers.size();
for (int i = 1; i < sz; i++)
{
//当前times值为非0
if (times != 0)
{
if (numbers[i] != ret)
{
times--;
}
else
{
times++;
}
}
//当前times值为0,原来的ret已经被不相等的值抵消
else
{
//把当前数组的值赋值给ret,tims重新置为1
ret = numbers[i];
times = 1;
}
}
return ret;
}
};
解法二
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
sort(numbers.begin(),numbers.end());
int sz=numbers.size();
int max=numbers[sz/2];//数组的中间数
return max;
}
};
边栏推荐
- 卸载手机自带APP的操作步骤
- 1.12 Web开发基础
- L. Link with Level Editor I dp
- 时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
- [Oracle SQL] calculate year-on-year and month on month (column to row offset)
- C language explanation series - comprehensive exercises, guessing numbers games
- 程序员如何改善精神内耗?
- Convolutional neural network (III) - target detection
- Calling mode and execution sequence of JS
- [2023 Jerry technology approval test questions in advance] ~ questions and reference answers
猜你喜欢

Leetcode:940. How many subsequences have different literal values

基于消防GIS系统的智慧消防应用

Solution to slow download speed of vagrant

Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)

【(SV && UVM) 笔试面试遇到的知识点】~ phase机制

Intelligent fire protection application based on fire GIS system

移动web

Embedded sharing collection 14

Recursive processing - subproblem

2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
随机推荐
Modifiers should be declared in the correct order
Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
英语句式参考纯享版 - 定语从句
Widget is everything, widget introduction
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
ETCD数据库源码分析——Cluster membership changes日志
Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
二叉树的性质 ~
数据库sql语言实战
[SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
Leetcode:934. The shortest Bridge
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
Convolutional neural network (IV) - special applications: face recognition and neural style transformation
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
[highly available MySQL solution] centos7 configures MySQL master-slave replication
How can programmers improve mental internal friction?
flex布局