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].

分析:

http://blog.csdn.net/itismelzp/article/details/51451374

bucket sort, 出现次数作为被sort的对象。

 public class Solution {
public List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// worst case, all values in nums are the same.
// Therefore, the size of buckets should be nums.length + 1
List<Integer>[] bucket = new List[nums.length + ]; for (int num : nums) {
map.put(num, map.getOrDefault(num, ) + );
} for (int key : map.keySet()) {
int value = map.get(key);
if (bucket[value] == null) {
bucket[value] = new ArrayList<Integer>();
}
bucket[value].add(key);
} List<Integer> res = new ArrayList<Integer>();
for (int i = bucket.length - ; i >= && res.size() < k; i--) {
if (bucket[i] != null) {
res.addAll(bucket[i]);
}
}
return res;
}
}

使用min heap.

 class Pair {
int num;
int count; public Pair(int num, int count) {
this.num = num;
this.count = count;
}
} public class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
// count the frequency for each element
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, ) + );
} // create a min heap
PriorityQueue<Pair> queue = new PriorityQueue<Pair>(new Comparator<Pair>() {
public int compare(Pair a, Pair b) {
return a.count - b.count;
}
}); // maintain a heap of size k.
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
Pair p = new Pair(entry.getKey(), entry.getValue());
queue.offer(p);
if (queue.size() > k) {
queue.poll();
}
} // get all elements from the heap
List<Integer> result = new ArrayList<Integer>();
while (queue.size() > ) {
result.add(queue.poll().num);
}
// reverse the order
Collections.reverse(result); return result;
}
}
 public class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
// count the frequency for each element
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, ) + );
} // create a min heap
Queue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<Map.Entry<Integer, Integer>>(new Comparator<Map.Entry<Integer, Integer>>() {
public int compare(Map.Entry<Integer, Integer> a, Map.Entry<Integer, Integer> b) {
return a.getValue() - b.getValue();
}
}); // maintain a heap of size k.
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
queue.offer(entry);
if (queue.size() > k) {
queue.poll();
}
} // get all elements from the heap
List<Integer> result = new ArrayList<Integer>();
while (queue.size() > ) {
result.add(queue.poll().getKey());
}
// reverse the order
Collections.reverse(result);
return result;
}
}

Top K Frequent Elements的更多相关文章

  1. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  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 = [ ...

  3. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

  4. 347. Top K Frequent Elements (sort map)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  5. [LeetCode] Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  6. 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 ...

  7. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  8. 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 ...

  9. 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 ...

  10. [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

随机推荐

  1. 关闭浏览器后Session失效原因分析

    参考文章:http://www.tuicool.com/articles/VNbYjqm 首先需要理解一下几点: 1.Http是无状态的,即对于每一次请求都是一个全新的请求,服务器不保存上一次请求的信 ...

  2. Maven-在eclipse创建maven项目

    在eclipse使用maven则需要给eclipse安装maven插件,具体安装maven插件安装相关文章 构建Maven项目 以eclipse3.6为例 1)创建简单Maven项目 点击Eclips ...

  3. 【POJ 1094】拓扑排序

    题意 给出n,代表有以A开始的n个字母,给出它们的m个小于关系(A<B).如果前i个关系可以确定n个字母的一个顺序就输出: Sorted sequence determined after i ...

  4. 【CodeForces 624D】Array GCD

    题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...

  5. shell 命令遇到的一些问题

    1.  command not found 一般都是未安装,需要root 权限去安装服务,就可正常使用.比如rz, sz, crontab, sendemail, lftp等 2. rz 传输失败,输 ...

  6. myeclipse 部署应用

    昨天把MyEclipse10给安装上了,今天想在MyEclipse下启动Tomcat并在浏览器中看到写的Web页面,但是当在浏览器中输入地址时,出现了404错误,出现这个错误的原因是因为没有找到指定的 ...

  7. 【基础语法】a++与++a的区别

    package com.on.learn.e2; /** * @author lj * 自增:a++与++a a++是指本行表达式不使用a自增后的值,++a是指本行开始就已经使用a自增后的值 * */ ...

  8. 文件流StreamReader和StreamWriter的使用

    using (StreamReader sr = new StreamReader(@"C:\Users\shuai\Desktop\文件流读取.txt", Encoding.De ...

  9. svn://59.46.115.123:13690/IOS/trunk/02.DevelopLib/04.Coding/uuridesharing

    svn://59.46.115.123:13690/IOS/trunk/02.DevelopLib/04.Coding/uuridesharing

  10. controller传值view

    400错误是请求错误 Model是map格式 @Controller public class HelloController { //view的值传给controller @RequestMappi ...