题目

https://leetcode.com/problems/top-k-frequent-elements/

题解

参考:leetcode 215. Kth Largest Element in an Array | 215. 数组中的第K个最大元素(Java)

class Solution {public static class HeapNode {int num;int count;public HeapNode(int num, Integer count) {this.num = num;this.count = count;}}public int[] topKFrequent(int[] nums, int k) {HashMap<Integer, Integer> map = new HashMap<>();for (int n : nums) {if (!map.containsKey(n)) map.put(n, 1);else map.put(n, map.get(n) + 1);}// 建大根堆int heapSize = map.size();HeapNode[] heap = new HeapNode[heapSize];int j = 0;for (int key : map.keySet())heap[j++] = new HeapNode(key, map.get(key));for (int i = heapSize - 1; i >= 0; i--) // 自下向上建堆 可证复杂度为O(n)heapify(heap, i, heapSize);// Top kint[] result = new int[k];int pos = 0;for (int i = 0; i < k; i++) {result[pos++] = heap[0].num;swap(heap, 0, --heapSize);heapify(heap, 0, heapSize);}return result;}public void heapify(HeapNode[] heap, int i, int heapSize) {int left = i * 2 + 1;while (left < heapSize) {int largest = left + 1 < heapSize && heap[left + 1].count > heap[left].count ? left + 1 : left; // 左右孩子较大者的下标largest = heap[largest].count > heap[i].count ? largest : i; // 两个孩子与父节点较大者的下标if (largest == i) break; // 不需要交换的情况swap(heap, largest, i);i = largest; // 更新i使其下沉left = 2 * i + 1;}}private void swap(HeapNode[] heap, int i, int j) {HeapNode tmp = heap[i];heap[i] = heap[j];heap[j] = tmp;}
}

leetcode 347. Top K Frequent Elements | 347. 前 K 个高频元素(大根堆)相关推荐

  1. 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...

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

  3. Leetcode - 347. Top K Frequent Elements(堆排序)

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

  4. [swift] 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, ...

  5. 力扣347:前k个高频元素---leetcode347:Top K Frequent Elements

    leetcode347题目链接:https://leetcode.cn/problems/top-k-frequent-elements 目录 一.题目描述 二.思路 1.什么是优先级队列呢? 2.什 ...

  6. 347. Top K Frequent Elements 前 K 个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...

  7. Leetcode: Top K Frequent Elements

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

  8. LeetCode Top K Frequent Elements

    题意:给出一个数组,求出前k个出现频率最多的数 思路:首先统计每个元素的次数(用Map),然后用堆排序只统计前k个(PriorityQueue) 代码如下: class Solution {class ...

  9. [Leetcode][第347题][JAVA][前K个高频元素][优先队列][堆][遍历set/map]

    [问题描述][中等] [解答思路] 1. 堆 复杂度 class Solution {public int[] topKFrequent(int[] nums, int k) {Map<Inte ...

最新文章

  1. 西安电子科技大学计算机录取分数,2021年西安电子科技大学投档线及各省最低录取分数线统计表...
  2. 腾讯视频如何删除观看记录
  3. DDNS实现内网服务器映射到公网(阿里云)
  4. VirtualBox不能共享剪贴板,有两种情形
  5. 卖零食怎么引流?怎么推广自己的零食产品,零食店怎么做引流?
  6. 日语中的-简体与敬体
  7. 设置cookie存活时间_Cookie所有知识点
  8. css层叠优先级,CSS样式的优先级(层叠)
  9. Linux利用sed批量自动化更换IP
  10. 第五人格为什么服务器维护中,《第五人格》提前停服原因和不删档测试说明
  11. java内存模型 infoq_深入理解 java 内存模型_程晓明_infoq.pdf
  12. 使用Fragment和ListView、RecyclerV实现动物信息显示
  13. 3月16日----3月20日一年级课程表
  14. iOS调试程序的方法
  15. mysql 回滚删除操作_Mysql如何快速回滚被删除的数据
  16. 使用echarts扩展地图案例
  17. python实现最大独立集算法_2018校招总结(外企,国内大公司,国内创业公司)...
  18. 计算机在职博士要考吗,在职博士容易考吗?
  19. RGP游戏的非主流应用——虚拟地图
  20. Linux压缩备份相关命令

热门文章

  1. CodeForces - 1373E Sum of Digits(贪心)
  2. POJ - 2449 Remmarguts' Date(第k短路:spfa+A*)
  3. python3列表_Python3列表
  4. pythonrequests解析_Python requests获取网页常用方法解析
  5. Jupyter-进阶教程
  6. 时序数据处理工具-时间序列数据特征提取TsFresh
  7. 33.文件与 IO.rs
  8. cocos2d-x初探学习笔记(14)--菜单项
  9. VC获取父进程PID
  10. 【白话科普】10s 从零看懂 H5