(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,2,3] and k = 2, return [1,2].
Note:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
public class Solution { //桶排序
public List<Integer> topKFrequent(int[] nums, int k) { //也可以使用PriorityQueue
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
List<Integer> res =new ArrayList<Integer>();
for(int num: nums){
map.put(num,map.getOrDefault(num,0)+1);
}
List<Integer>[] bucket=new List[nums.length+1];
for(int key : map.keySet()){
int freq=map.get(key);
if(bucket[freq]==null){
bucket[freq]=new ArrayList<Integer>();
}
bucket[freq].add(key);
}
for(int i=nums.length;i>=0 && res.size()<k;i--){
if(bucket[i]!=null)
res.addAll(bucket[i]);
}
return res;
}
}Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
(Collection)347. Top K Frequent Elements的更多相关文章
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 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 ...
随机推荐
- 解决python字典结构内存暴涨问题
背景:当读取一个key value数据的时候,python的字典结构会造成内存使用扩10倍左右,无可容忍.此文解决这个问题 数据:word2vec训练的结果,word对应400维的词向量.词表共1.6 ...
- ubuntu随笔
在命令行里输入 sudo nautilus 之后输入你的用户的密码,会弹出一个目录窗口来,可以复制到这来
- Mark Down绘图语法
以下语法在网易云笔记中测试通过. 绘图的标志位是三个单引号``` 开始 ``` 结尾 ,注意是英文半角的单引号,以下的字符也是英文半角状态下的才正确. 搜狗输入法的要特别注意,记得把shift 切 ...
- 用canvas生成二维码
$("#actimg").qrcode({ render: "canvas", //设置渲染方式,有tabl ...
- 原生js tab 栏切换
<div id="box"> <div> <button>按钮1</button> <button>按钮2</bu ...
- JS和Android交互
//本地webview写法 webview = (WebView) findViewById(R.id.webview); webview.loadUrl("http://192.168.1 ...
- vs2010配置boost编程环境(照抄并简化)
第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_50_0.7z. 第二步:在D盘(我C盘空间不大,准备出至少10G的空间吧)创建 ...
- 灰色蓝色系简洁自适应登录HTML页面
自己写了一个简介的登录页面,页面背景为灰色,居中为登录框,登录面板为半透明效果,整体十分美观,登录按钮有JS效果.自己感觉还不错,拿出来分享一下. 页面效果图 源码下载链接:http://files. ...
- 深度|OpenAI 首批研究成果聚焦无监督学习,生成模型如何高效的理解世界(附论文)
本文经机器之心(微信公众号:almosthuman2014)授权转载,禁止二次转载,原文. 选自 Open AI 作者:ANDREJ KARPATHY, PIETER ABBEEL, GREG BRO ...
- Qt 多线程和网络编程学习
一,Qt多线程类学习 QThread类,开始一个新的线程就是开始执行重新实现QThread::run(),run()是默认现实调用exec(),QThread::start()开始线程的执行,run( ...