347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].
Note:
- 347. Top K Frequent ElementsYou may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
========
题目:
因为c++中提供的哈希表map,不能按照value进行排序,
我们需要一个按照value值进行排序的数据结构,所以需要自定义一个.
定义排序函数,static bool mykeyval(const keyval &l, const keyval &r){}
利用自带的sort()函数就可以了.
====
代码
class Solution {
public:
struct keyval{
int key;//数字
int val;//数字出现的次数
keyval(int k=,int v=):key(k),val(v){}
};
static bool mykeyval(const keyval &l,const keyval &r){
return l.val>r.val;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int,int> m;///key,multis
vector<int> re;
for(auto i: nums){
m[i]++;
}
vector<keyval> tmp(m.size());
int d = ;
for(auto i: m){
tmp[d].key = i.first;
tmp[d++].val = i.second;
}
sort(tmp.begin(),tmp.end(),mykeyval);
for(int i = ;i<k;i++){
re.push_back(tmp[i].key);
}
return re;
}
};
347. Top K Frequent Elements的更多相关文章
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- LeetCode 【347. Top K Frequent Elements】
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- Leetcode 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [LC] 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
- (Collection)347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
随机推荐
- 隐藏select最右侧的下拉三角图标的css样式
-webkit-appearance:none; -moz-appearance:none; appearance:none;
- Spring AOP 实现功能权限校验功能
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...
- c 函数及指针学习 8
联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> union sa { double a; int b; ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- Reverse a Singly LinkedList
Reverse a Singly LinkedList * Definition for singly-linked list. * public class ListNode { * int val ...
- 用360安全浏览器控制网速,调试loading
360安全浏览器 按f12 两个按钮的意思分别为禁止缓存,网络设置,这样就能控制网速了,调试loading了
- 论文笔记之:From Facial Parts Responses to Face Detection: A Deep Learning Approach
From Facial Parts Responses to Face Detection: A Deep Learning Approach ICCV 2015 从以上两张图就可以感受到本文所提方法 ...
- git checkout 和 git checkout --merge <branch_name>使用
一.git checkout //查看当前分支$ git branch master *t2 testing //checkout会覆盖当前工作区文件和覆盖暂存区内容,所以发现分支有未提交的警告,执行 ...
- 代码里面执行bat
public static void executeBat(String path) { try { File file = new File(path); ...
- UserDefault的使用,保存小数据到本地-iOS
//保持到本地数据 NSArray *array=@[@"234",@"sdfe"]; NSUserDefaults *userDefault=[NSUserD ...