当前位置:网站首页>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;
}

边栏推荐
猜你喜欢

Vulnerability recurrence - Net ueeditor upload

RichView RichEdit SRichViewEdit PageSize 页面设置与同步

王者战力查询改名工具箱小程序源码-带流量主激励广告

Niuke programming question -- must brush the string of 101 (brush the question efficiently, draw inferences from one instance)
![[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)](/img/bf/524e78473625a31c024783ccec8d46.png)
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)

Big factories are wolves, small factories are dogs?

目标检测——Yolo系列

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

【多线程】 实现单例模式 ( 饿汉、懒汉 ) 实现线程安全的单例模式 (双重效验锁)

Entering Ruxin Town, digital intelligence transformation connects "future community"
随机推荐
docker ubuntu容器中安装mysql遇到的问题
RichView 文档中的 ITEM
2022熔化焊接与热切割上岗证题目模拟考试平台操作
想得到股票开户的优惠链接,如何得知?在线开户是安全么?
Écrire un document de blog
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
2022安全员-B证考试练习题模拟考试平台操作
Is it safe to open an account online? Can a novice open a stock trading account.
Data analysts sound tall? Understand these points before you decide whether to transform
Keras machine translation practice
Myslq ten kinds of locks, an article will take you to fully analyze
Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)
Iframe 父子页面通信
internship:逐渐迈向项目开发
运动捕捉系统原理
漏洞复现-.Net-ueditor上传
What did you learn about cheating after you went to college?
Importance of EDA tools to chip industry knowledge popularization
关联线探究,如何连接流程图的两个节点
Items in richview documents