力扣 - 347. 前 K 个高频元素
题目
思路1(哈希表与排序)
- 先用哈希表记录所有的值出现的次数
- 然后将按照出现的次数进行从高到低排序
- 最后取前 k 个就是答案了
代码
class Solution {
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> hashtable = new HashMap<>();
// 统计每个次数出现的次数
for (int num : nums) {
hashtable.put(num, hashtable.getOrDefault(num, 0) + 1);
}
// 排序
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(hashtable.entrySet());
list.sort(new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
// 获取前k高的结果
int[] res = new int[k];
for (int i = 0; i < k; i++) {
res[i] = list.get(i).getKey();
}
return res;
}
}
复杂度分析
- 时间复杂度:\(O(NlogN)\),其中 N 为数组长度,遍历一遍花的时间是N,但是由于排序的时间复杂度是NlogN,所以总的时间复杂度为NlogN
- 空间复杂度:\(O(N)\),其中 N 为数组长度
思路2(建堆)
- 也是先用哈希表统计出现的次数
- 然后利用小顶堆获取前k高的元素,堆操作的时间复杂度为logk,所以总的就是Nlogk
代码
class Solution {
public int[] topKFrequent(int[] nums, int k) {
// 用哈希表存储出现次数
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int num : nums) {
hashtable.put(num, hashtable.getOrDefault(num, 0) + 1);
}
// 使用优先队列
PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] - o2[1];
}
});
// 小顶堆,容量只为k,存储前n大的元素
for (Map.Entry<Integer, Integer> entry : hashtable.entrySet()) {
int element = entry.getKey();
int count = entry.getValue();
if (queue.size() == k) {
// 保证队列存储的前k个元素出现频率都是最多的
if (queue.peek()[1] < count) {
queue.poll();
queue.offer(new int[]{element, count});
}
} else {
queue.offer(new int[]{element, count});
}
}
// 将优先队列元素出队转换为结果
int[] res = new int[k];
for (int i = 0; i < k; i++) {
res[i] = queue.poll()[0];
}
return res;
}
}
复杂度分析
- 时间复杂度:\(O(Nlogk)\),其中 N 为数组长度,由于堆的大小至多为 k,因此每次堆操作需要 O(logk) 的时间,共需 O(Nlogk) 的时间
- 空间复杂度:\(O(N)\),其中 N 为数组长度
力扣 - 347. 前 K 个高频元素的更多相关文章
- 力扣347——前 K 个高频元素
这道题主要涉及的是对数据结构里哈希表.小顶堆的理解,优化时可以参考一些排序方法. 原题 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2, ...
- 代码随想录第十三天 | 150. 逆波兰表达式求值、239. 滑动窗口最大值、347.前 K 个高频元素
第一题150. 逆波兰表达式求值 根据 逆波兰表示法,求表达式的值. 有效的算符包括 +.-.*./ .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 注意 两个整数之间的除法只保留整数部分. ...
- Java实现 LeetCode 347 前 K 个高频元素
347. 前 K 个高频元素 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输 ...
- [LeetCode]347. 前 K 个高频元素(堆)
题目 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1 ...
- leetcode 347. 前 K 个高频元素
问题描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums ...
- Leetcode 347.前K个高频元素 By Python
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- 347. 前K个高频元素
题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = ...
- leetcode.排序.347前k个高频元素-Java
1. 具体题目 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums ...
- [LeetCode] 347. 前K个高频元素
python 版方法1:链表 class Solution(object): def topKFrequent(self, nums, k): """ :type num ...
随机推荐
- js class static property & public class fields & private class fields
js class static property class static property (public class fields) const log = console.log; class ...
- SVG image tag
SVG image tag https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_Image_Tag <?xml versi ...
- 一文搞懂 js 中的各种 for 循环的不同之处
一文搞懂 js 中的各种 for 循环的不同之处 See the Pen for...in vs for...of by xgqfrms (@xgqfrms) on CodePen. for &quo ...
- Web Share API
Web Share API https://w3c.github.io/web-share/ Web Share API, W3C Editor's Draft 15 April 2020 https ...
- AMP ⚡
AMP https://amp.dev/zh_cn/ PWA AMP Playground https://playground.amp.dev/?runtime=amp4email <!doc ...
- windows 设置右键菜单
编辑注册表 在文件 右键菜单中添加 xx.reg: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\vscode] &q ...
- c++ 去掉字符串首尾空格
http://www.cplusplus.com/reference/regex/regex_replace/ #include <iostream> #include <regex ...
- 【PY从0到1】 一文掌握Pandas量化进阶
# 一文掌握Pandas量化进阶 # 这节课学习Pandas更深的内容. # 导入库: import numpy as np import pandas as pd # 制作DataFrame np. ...
- dategrip的使用技巧
原文链接:https://blog.csdn.net/weixin_44421461/article/details/109541903 数据表复制,可以直接用sql语句 1.复制表结构及数据到新表 ...
- SpringBoot读取资源目录下的文件
需要读取resources目录下的文件,那么方法如下: 假设在资源目录下的template目录下有一个文件a.txt,获取到文件流的方式 InputStream stream = this.getCl ...