当前位置:网站首页>Leetcode 2190. The number that appears most frequently in the array immediately after the key (yes, once)
Leetcode 2190. The number that appears most frequently in the array immediately after the key (yes, once)
2022-06-12 16:35:00 【I'm not xiaohaiwa~~~~】
I'll give you a subscript from 0 The starting array of integers nums , I'll give you an integer at the same time key , It's in nums There have been .
Statistics stay nums The array is followed by key Different integers that appear later target Number of occurrences of . In other words ,target The number of occurrences of is i Number of :
- 0 <= i <= n - 2
- nums[i] == key And
- nums[i + 1] == target .
Please return and appear most Times target . The test data guarantees the most frequent occurrence of target Is the only one. .
Example 1:
Input :nums = [1,100,200,1,100], key = 1
Output :100
explain : about target = 100 , In subscript 1 and 4 There has been 2 Time , And they all follow key .
There are no other integers in key Followed by , So we go back to 100 .
Example 2:
Input :nums = [2,2,2,2,3], key = 2
Output :2
explain : about target = 2 , In subscript 1 ,2 and 3 There has been 3 Time , And they all follow key .
about target = 3 , In subscript 4 Have you ever appeared 1 Time , And follow key .
target = 2 Yes, follow closely key The number that appears most frequently after , So we go back to 2 .
Tips :
- 2 <= nums.length <= 1000
- 1 <= nums[i] <= 1000
- The test data ensure that the answer is unique .
Main idea : utilize map To store
Code:
class Solution {
public:
typedef pair<int, int> PAIR;
struct CmpByValue {
bool operator()(const PAIR& lhs, const PAIR& rhs) {
return lhs.second > rhs.second;
}
};
int mostFrequent(vector<int>& nums, int key) {
map<int,int>mymap;
for(int i=0;i<nums.size();i++)
{
if(nums[i]==key)
{
if((i+1)<nums.size())
{
mymap[nums[i+1]]++;
}
}
}
// hold map Elements in are transferred to vector in
vector<PAIR> vec(mymap.begin(), mymap.end());
// Yes vector Sort
sort(vec.begin(), vec.end(), CmpByValue());
return vec[0].first;
}
};
边栏推荐
- D structure as index of multidimensional array
- Leetcode 2194. Cellules dans une plage dans un tableau Excel (OK, résolu)
- 【BSP视频教程】BSP视频教程第17期:单片机bootloader专题,启动,跳转配置和调试下载的各种用法(2022-06-10)
- HEMA is the best representative of future retail
- JS écoute si l'utilisateur allume le focus de l'écran
- Statistical machine learning code set
- Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency
- generate pivot data 2
- 程序的动态加载和执行
- Interview: hashcode() and equals()
猜你喜欢
Overview of webrtc's audio network Countermeasures
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
双写一致性问题
What's the matter with pbootcms' if judgment failure and direct display of labels?
Statistical machine learning code set
批量--03---CmdUtil
pbootcms的if判断失效直接显示标签怎么回事?
acwing 801. Number of 1 in binary (bit operation)
[Hunan University] information sharing of the first and second postgraduate entrance examinations
Kill program errors in the cradle with spotbugs
随机推荐
generate pivot data 1
calibration of sth
Acwing 1927 自动补全(知识点:hash,二分,排序)
HEMA is the best representative of future retail
[fishing artifact] UI library second change lowcode tool -- List part (I) design and Implementation
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.2 低级 I/O(read 和 write)
Sum of acwing796 submatrix
What's the matter with pbootcms' if judgment failure and direct display of labels?
看《梦华录》上头的人都该尝试下这款抖音特效
acwing 801. Number of 1 in binary (bit operation)
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
The C programming language (version 2) notes / 8 UNIX system interface / 8.3 open, create, close, unlink
<山东大学项目实训>渲染引擎系统(五)
《安富莱嵌入式周报》第268期:2022.05.30--2022.06.05
1.delete
The C programming language (version 2) notes / 8 UNIX system interfaces / 8.6 instances (directory list)
vim 从嫌弃到依赖(16)——宏
<山东大学项目实训>渲染引擎系统(七)
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.4 随机访问(lseek)
How to construct PostgreSQL error codes