题目描述:

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.

解题思路:

使用HashMap计数

代码如下:

public class Solution {
public List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer>[] freqArray = new List[nums.length + 1]; for(int num : nums){
map.put(num, map.getOrDefault(num, 0) + 1);
} for(int key : map.keySet()){
int freq = map.get(key);
if(freqArray[freq] == null)
freqArray[freq] = new ArrayList<Integer> ();
freqArray[freq].add(key);
} List<Integer> res = new ArrayList<>(); for(int i = freqArray.length - 1; i >= 0 & res.size() < k; i--){
if(freqArray[i] != null)
res.addAll(freqArray[i]);
} return res; }
}

  

Java [Leetcode 347]Top K Frequent Elements的更多相关文章

  1. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  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 = [ ...

  3. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

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

  4. [LeetCode] 347. Top K Frequent Elements 解题思路 - Java

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

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

  6. [leetcode]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 ...

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

  8. 347. Top K Frequent Elements (sort map)

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

  9. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

随机推荐

  1. HDU 4370 - 0 or 1 (SPFA+思维)

    题意:给一个N*N的矩阵C,和一个N*N的只由0和1组成的矩阵X. X满足以下条件: 1.X 12+X 13+...X 1n=1  2.X 1n+X 2n+...X n-1n=1  3.任意 i (1 ...

  2. ASYNCAPI

    https://www.asyncapi.com Introduction AsyncAPI provides a specification that allows you to define Me ...

  3. Kotlin学习记录2

    参考我的博客:http://www.isedwardtang.com/2017/09/03/kotlin-primer-2/

  4. 在 Mac OS 上编译 OBS

    本文转自:在 Mac OS 上编译 OBS | www.samirchen.com 安装环境 第一步,做准备工作,安装编译 OBS 所需要的环境,流程如下: // 给当前用户添加 /usr/local ...

  5. 会话控制Session的应用

    Session技术与Cookie相似,都是用来存储使用者的相关资料.但是最大不同之处在于Cookie是将数据存放于客户端计算机中,而Session则是将数据存放于服务器系统下. 在Web技术发展史上, ...

  6. 技巧.【转】在虚拟机Vmware中使用HID设备(如USB免驱键盘)

    ZC:我的环境:Win7x64.VMware10 ZC:我的处理: ZC: (1).usb.generic.allowHID = "TRUE" (本来就有,将它的位置提前) ZC: ...

  7. lucene学习-2 - 一个示例

    接下来我会写一个lucene的实例.实际上在搜索引擎上随便搜索下都能找到这样的东西.不过还是写一下吧,这也是我学习的经历. package com.zhyea.doggie; import java. ...

  8. hermite插值

    Hermite 插值就是要求插值函数不仅经过所给节点,而且要保证在该点的导数也相等.<备注:虽然还不理解这句话,但是还是先放这里!> 所谓样条曲线(Spline Curves)是指给定一组 ...

  9. javascript 对象简单介绍(二)

    JavaScript Array(数组) 对象数组对象的作用是:使用单独的变量名来存储一系列的值. 什么是数组?数组对象是使用单独的变量名来存储一系列的值.如果你有一组数据(例如:车名字),存在单独变 ...

  10. Android DDMS ADB启动失败错误解决!

    ADB server didn't ACK && make sure the plugin is properly configured! adb启动失败一般是端口被占用! 解决方法和 ...