【问题描述】[中等]
【解答思路】
1. 堆
复杂度
class Solution {public int[] topKFrequent(int[] nums, int k) {Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();for (int num : nums) {occurrences.put(num, occurrences.getOrDefault(num, 0) + 1);}// int[] 的第一个元素代表数组的值,第二个元素代表了该值出现的次数PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {public int compare(int[] m, int[] n) {return m[1] - n[1];}});for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {int num = entry.getKey(), count = entry.getValue();if (queue.size() == k) {if (queue.peek()[1] < count) {queue.poll();queue.offer(new int[]{num, count});}} else {queue.offer(new int[]{num, count});}}int[] ret = new int[k];for (int i = 0; i < k; ++i) {ret[i] = queue.poll()[0];}return ret;}
}
class Solution {public int[] topKFrequent(int[] nums, int k) {Map<Integer,Integer> map =new HashMap<>();for(int num:nums){map.put(num,map.getOrDefault(num,0)+1);}PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->map.get(a)-map.get(b));/*PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {return map.get(b) -map.get(a);}});*/for(int key:map.keySet()){queue.add(key);if(queue.size()>k){queue.poll();}}int res[]=new int[k];int index=0;while(k>0){res[index++]=queue.poll();k--;}return res;}
}
【总结】
1. 大小堆的建立(其他类比)
1.1 Map的小堆
PriorityQueue queue = new PriorityQueue<>((a,b)->map.get(a)-map.get(b));
1.2 Map的大堆
//idea Comparator 自动生成
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {return map.get(b) -map.get(a);}});
1.3 分开写
// Customer 一个class 含id 7 初始化大小
Queue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator);
//匿名Comparator实现public static Comparator<Customer> idComparator = new Comparator<Customer>(){@Overridepublic int compare(Customer c1, Customer c2) {return (int) (c1.getId() - c2.getId());}};
2.遍历map /set
map
public static void main(String[] args) {// 构建一个Map 初始值为3条数据Map<String, String> map = new HashMap<String, String>();map.put("1", "xiaqiu");map.put("2", "pangzi");map.put("3", "shouzi");//第一种:普遍使用,二次取值System.out.println("通过Map.keySet遍历key和value:");for (String key : map.keySet()) {System.out.println("key= "+ key + " and value= " + map.get(key));}//第二种:通过Iterator迭代器遍历循环Map.entrySet().iterator();System.out.println("通过Map.entrySet使用iterator遍历key和value:");Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();while (it.hasNext()) {Map.Entry<String, String> entry = it.next();System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//第三种:笔者推荐,尤其是容量大时(相对来说 比2好一点 效率高)System.out.println("通过Map.entrySet遍历key和value");for (Map.Entry<String, String> entry : map.entrySet()) {System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//第四种System.out.println("通过Map.values()遍历所有的value,但不能遍历key");for (String v : map.values()) {System.out.println("value= " + v);}}
set
1.迭代遍历:
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) { String str = it.next(); System.out.println(str);
} 2.for循环遍历:
for (String str : set) { System.out.println(str);
}
3.堆的思想
【数据结构与算法】堆
参考链接:https://www.cnblogs.com/magicya/p/6683052.html
参考链接:https://www.cnblogs.com/XQiu/p/5087961.html