347. Top K Frequent Elements 最常用的k个元素
[抄题]:
Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3]
and k = 2, return [1,2]
.
Note:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
List<Integer>[] bucket列表组的空间是n+1,因为可能出现最小0次、最大n次的情况。
[思维问题]:
想不到木桶排序:统计出现次数相同的情况,所以可以用。空间是n+1,因为可能出现最小0次
[英文数据结构或算法,为什么不用别的数据结构或算法]:
bucket.length - 1 index其实就是出现的次数
Map.getOrDefault(n, 0) 有就是前一个,没有就是后一个
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- if (bucket[i] != null)链表非空才能加,否则NPE
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
想不到木桶排序:统计出现次数相同的情况,所以可以用。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
//initialization: result, HashMap, List<Integer> [] Bucket
List<Integer> result = new ArrayList<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer> [] bucket = new List[nums.length + 1]; //corner case
if (nums == null || nums.length == 0 || k <= 0) return result; //count the frequence into map
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
int n = map.get(nums[i]);
map.put(nums[i], n + 1);
}else {
map.put(nums[i], 1);
}
} //put all the map's key into Bucket
for (int key : map.keySet()) {
if (bucket[map.get(key)] == null)
bucket[map.get(key)] = new ArrayList();
bucket[map.get(key)].add(key);
} //get answer from Bucket
for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) {
if (bucket[i] != null)
result.addAll(bucket[i]);
} //return
return result;
}
}
347. Top K Frequent Elements 最常用的k个元素的更多相关文章
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 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,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,2 ...
- [LC] 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
随机推荐
- xamarin android 报错 Could not load assembly 'Xamarin.Android.Support.v7.AppCompat
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 Exception while loading assemblies: System.IO.FileNotFoundException: Cou ...
- 《Linux内核原理与分析》第四次作业
跟踪分析Linux内核的启动过程 使用实验楼的虚拟机打开shell 使用 gdb 跟踪调试内核 使用 qemu qemu -kernel linux-3.18.6 /arch/x86/boot/baI ...
- C语言编程知识点
(1)预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题):#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL 1) #defin ...
- [转]ANTS Performance Profiler和ANTS Memory Profiler 使用
.NET性能调优之一:ANTS Performance Profiler的使用 .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使 ...
- Delphi在调WebService的时候加Soap头验证
procedure ws: WebServiceSoap; H: XXXHeader; begin ws := GetWebServiceSoap; H := XXXHeader.Cr ...
- 基础:位(bit)、字节(byte)、字符、编码之间的关系
1.位: 数据存储的最小单位.每个二进制数字0或者1就是1个位: 2.字节: 8个位构成一个字节:即:1 byte (字节)= 8 bit(位): 1 KB = 1024 B(字节): 1 MB = ...
- [蓝桥杯]ALGO-186.算法训练_P0501
输入两个无符号整数x, y, 用位操作实现无符号整数的乘法运算.不用考虑整数的溢出. 输入: 输出: 题目描述 代码如下: #include <stdio.h> #include < ...
- [转][C#]文件流读取
{ internal static class FileUtils { public static string GetRelativePath(string absPath, string base ...
- 【oracle常见错误】oracle监听程序配置/“ORA-12541: TNS: 无监听程序”
问题描述 在用PL/SQL Developer连接Oracle 11g时报错“ORA-12541: TNS: 无监听程序”,如下图所示.可以按照如下的步骤进行解决. 解决方案 监听程序配置 从开始菜单 ...
- feign接口调用异常的解决方向
1. consul: 检查调用方服务与被调用方服务是否在同一个consul; 2. swagger: 检查swagger注释是否清晰.恰当: 比如: @ApiImplicitParams(value ...