当前位置:网站首页>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;
}
};边栏推荐
- Cut rope / integer split
- When can Flink support the SQL client mode? When can I specify the applicati for submitting tasks to yarn
- 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
- knife4j配置使用直接拷贝即可
- 堆排序与加强堆代码 用于记忆
- LeetCode:1037. Effective boomerang - simple
- Mysql database foundation
- pytorch深度学习——卷积操作以及代码示例
- The most common habits from more than 200 English papers written by gradua
- Mixin -- mixed
猜你喜欢

Tutoriel Microsoft Word "5", comment changer les marges de page et créer une barre de nouvelles en word?

Four methods to obtain the position index of the first n values of the maximum and minimum values in the list

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.

pytorch深度学习——卷积操作以及代码示例

KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~

Quick start to elastic job, three minutes to experience distributed scheduled tasks

Pytorch deep learning -- convolution operation and code examples

Microsoft Word tutorial, how to change page orientation and add borders to pages in word?

魔塔类游戏实现源码及关卡生成

Microsoft Word 教程「5」,如何在 Word 中更改頁邊距、創建新聞稿欄?
随机推荐
【电脑使用】如何设置没有自启项的软件开机启动
Why do some web page style attributes need a colon ":" and some do not?
Vertical bar of latex tips absolute value \left\right|
In depth learning experience and tools
国庆期间给大家推荐一个可能会成为2019最佳的CRUD工具
AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
Analysis on rendering principle of mobile terminal
[observation] shengteng Zhixing: scene driven, innovation first, press the "acceleration key" for Intelligent Transportation
Heap sorting and hardening heap code for memory
LeetCode:1037. 有效的回旋镖————简单
自注意力(self-attention)和多头注意力(multi-head attention)
终于有人说清楚了Cookie、Session、Token的区别。详解,面试题。
35岁被裁员,还能拥有美妙人生吗?
NetWorkX使用方法及 nx.draw()相关参数
Service management and communication, basic principle analysis
深度学习调参经验和工具
Identity and access management (IAM)
What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
vulnhub-The Planets: Earth
在阿里云国际上使用 OSS 和 CDN 部署静态网站