当前位置:网站首页>Niuke.com: numbers that appear more than half of the times in the array
Niuke.com: numbers that appear more than half of the times in the array
2022-06-10 21:00:00 【lsgoose】


Catalog
Method 1 : Hash
We can use a storage < Numbers , Number of occurrences > To determine whether a number appears more than half the time .
First traverse the output times , And then traverse to find the number of occurrences more than half .
The code is as follows :
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
unordered_map<int, int> mp;
for(const int n : numbers){
mp[n]++;
}
for(const int val : numbers){
if(mp[val]>numbers.size()/2){
return val;
}
}
return 0;
}
};Method 2 : Sort
Because this number appears more than half of the time , So we sort it out , Then take the median .
The code is as follows :
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
sort(numbers.begin(), numbers.end());
// int cond=numbers[numbers.size()/2];
// int cnt=0;
// for(const int k : numbers){
// if(cond==k) cnt++;
// }
// if(cnt > numbers.size()/2) return cond;
// return 0;
return numbers[numbers.size()/2];
}
};Because the problem is guaranteed to be solved , So we can directly return the median after sorting , If there is no guarantee of solvability , You also need to iterate through the array to see if the number of occurrences exceeds half .
O(nlogn) Time complexity of
Method 3 : The candidate
This method is quite mysterious , Is to take the mode and other numbers to offset , The last remaining number is the number that we are looking for more than half of the times .
The code is as follows :
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int cond=-1;
int cnt=0;
for(int i=0;i<numbers.size();++i){
if(cnt==0){
cond=numbers[i];
++cnt;
}else{
if(cond==numbers[i]){
++cnt;
}else{
--cnt;
}
}
}
return cond;
}
};边栏推荐
- 服务管理与通信,基础原理分析
- Is Jiuzhou futures regular? Is it safe to open an account
- Mixin -- mixed
- Analysis on rendering principle of mobile terminal
- MySQL service startup failed
- How to use Diablo immortal database
- H5 van popup full screen pop-up window, simulates the page fallback effect, supports the return button in the upper left corner, and is suitable for physical return, side sliding and bottom return key
- What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
- Solve the problem that the idea automatically becomes * when there are more than 5 identical packages
- 观点丨Play and Earn 会让加密游戏误入歧途
猜你喜欢

六级考试-商务英语-考前最后一背

Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.

「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0

Talk about server performance optimization ~ (recommended Collection)

The most common habits from more than 200 English papers written by gradua

Recommend a crud tool that may become the best crud tool in 2019 during the National Day

You have to learn math to play art?

LeetCode:1037. 有效的回旋镖————简单

synergy: server refused client with our name

京东发布基于张量网络加速的大规模、分布式量子机器学习平台TeD-Q
随机推荐
P5723 【深基4.例13】质数口袋
72. 编辑距离 ●●●
牛客网:数组中出现次数超过一半的数字
Talk about server performance optimization ~ (recommended Collection)
PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
Self attention and multi head attention
堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡
[computer use] how to set software startup without auto startup
What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
Redis cluster form - sentry mode cluster and high availability mode cluster - redis learning notes 003
Magic tower game implementation source code and level generation
Can you still have a wonderful life if you are laid off at the age of 35?
Analysis on rendering principle of mobile terminal
Mixin -- mixed
LeetCode:1037. 有效的回旋镖————简单
国庆期间给大家推荐一个可能会成为2019最佳的CRUD工具
synergy: server refused client with our name
In depth learning experience and tools
CET-6 - Business English - the last recitation before the test