赞
踩
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
(1)借助 哈希表 来建立数字和其出现次数的映射,遍历一遍数组统计元素的频率
(2)维护一个元素数目为 k的最小堆
(3)每次都将新的元素与堆顶元素(堆中频率最小的元素)进行比较
(4)如果新的元素的频率比堆顶端的元素大,则弹出堆顶端的元素,将新的元素添加进堆中
(5)最终,堆中的 k个元素即为前 k个高频元素
https://leetcode-cn.com/problems/top-k-frequent-elements/solution/leetcode-di-347-hao-wen-ti-qian-k-ge-gao-pin-yuan-/
class Solution { public List<Integer> topKFrequent(int[] nums, int k) { // 使用字典,统计每个元素出现的次数,元素为键,元素出现的次数为值 HashMap<Integer,Integer> map = new HashMap(); for(int num : nums){ if (map.containsKey(num)) { map.put(num, map.get(num) + 1); } else { map.put(num, 1); } } // 遍历map,用最小堆保存频率最大的k个元素 PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return map.get(a) - map.get(b); } }); for (Integer key : map.keySet()) { if (pq.size() < k) { pq.add(key); } else if (map.get(key) > map.get(pq.peek())) { pq.remove(); pq.add(key); } } // 取出最小堆中的元素 List<Integer> res = new ArrayList<>(); while (!pq.isEmpty()) { res.add(pq.remove()); } return res; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。