当前位置:网站首页>leetcode刷题:栈与队列06(前 K 个高频元素)
leetcode刷题:栈与队列06(前 K 个高频元素)
2022-07-01 19:16:00 【涛涛英语学不进去】
347.前 K 个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
示例 1:
- 输入: nums = [1,1,1,2,2,3], k = 2
- 输出: [1,2]
示例 2:
- 输入: nums = [1], k = 1
- 输出: [1]
提示:
- 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
- 你的算法的时间复杂度必须优于 O ( n log n ) O(n \log n) O(nlogn) , n 是数组的大小。
- 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。
- 你可以按任意顺序返回答案。
又是一个中等题一发入魂,飘了hhhhh
思路:
哈希表计算每个数字出现的次数
键、值分别放到两个数组,按值排序,键镜像同步变换位置
返回前K个数字。
package com.programmercarl.stacks_queues;
import java.util.HashMap;
/** * @ClassName TopKFrequent * @Descriotion TODO * @Author nitaotao * @Date 2022/6/30 14:13 * @Version 1.0 * https://leetcode.cn/problems/top-k-frequent-elements/ * 347. 前 K 个高频元素 **/
public class TopKFrequent {
public static int[] topKFrequent(int[] nums, int k) {
/** * 思路: * 哈希表计数 */
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
int[] keys = new int[map.size()];
int[] values = new int[map.size()];
int index = 0;
for (Integer key : map.keySet()) {
keys[index] = key;
values[index] = map.get(key);
index++;
}
//value 排序,key 镜像同步交换
for (int i = 0; i < values.length-1; i++) {
for (int j = i+1; j < values.length; j++) {
if (values[i] < values[j]) {
int tempValue = values[i];
values[i] = values[j];
values[j] = tempValue;
int tempKey = keys[i];
keys[i] = keys[j];
keys[j] = tempKey;
}
}
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = keys[i];
}
return result;
}
}

看题解还有另一个思路,是我们之前上课时老师讲的思路,大顶堆思想。
public static int[] topKFrequent(int[] nums, int k) {
/** * 思路: * 哈希表计数 */
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
//大顶堆思想
//优先级队列,按值大小排序
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
for(Map.Entry<Integer,Integer>entry : map.entrySet()) {
queue.offer(entry);
//只保留前k个数
if (queue.size() > k) {
queue.poll();
}
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = queue.poll().getKey();
}
return result;
}

边栏推荐
- Customize the insertion of page labels and realize the initial search of similar address books
- 2022/6/8-2022/6/12
- Use Zadig to build a continuous delivery platform from 0 to 1
- 目标检测——Yolo系列
- internship:逐渐迈向项目开发
- internship:复杂json格式数据编写接口
- There are four ways to write switch, you know
- 想得到股票开户的优惠链接,如何得知?在线开户是安全么?
- 8K HDR!|为 Chromium 实现 HEVC 硬解 - 原理/实测指南
- Iframe 父子页面通信
猜你喜欢

Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应

目标检测——Yolo系列

Redis installation and startup in Windows environment (background startup)

极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)

编译原理复习笔记

RichView RichEdit SRichViewEdit PageSize 页面设置与同步

Niuke programming question -- must brush the string of 101 (brush the question efficiently, draw inferences from one instance)

Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it

《软件工程导论(第六版)》 张海藩 复习笔记

On the next generation entrance of the metauniverse -- the implementation of brain computer interface
随机推荐
EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
How to create a pyramid with openmesh
Penetration tools - trustedsec's penetration testing framework (PTF)
目标检测——Yolo系列
【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
随机头像大全,多分类带历史记录微信小程序源码_支持流量主
[mysql] install mysql5.7
[Blue Bridge Cup web] analysis of the real topic of the 13th Blue Bridge Cup web university group match in 2022
Importance of EDA tools to chip industry knowledge popularization
2022年高处安装、维护、拆除考题模拟考试平台操作
Richview RichEdit srichviewedit PageSize page setup and synchronization
How to turn off the boot auto start software in win11
Keras机器翻译实战
uniapp使用腾讯地图选点 没有window监听回传用户的位置信息,怎么处理
薛定谔的日语学习小程序源码
宅男壁纸大全微信小程序源码-带动态壁纸支持多种流量主
RichView TRVDocParameters 页面参数设置
math_利用微分算近似值
8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide
如果浏览器被意外关闭,react怎么缓存用户填写的表单?