题目描述:

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. redis 笔记02 对象、数据库

    对象 Redis并没有使用之前介绍的数据结构来实现键值对数据库,而是基于那些数据结构创建了一个对象系统,这个系统包含字符串对象.列表对象.哈希对象.集合对象和有序集合对象这五种类型对象, 每种对象都用 ...

  2. GIT使用—一些概念

    (1)Git版本库(repository) 一个简单的数据库,包括所有用来维护与管理项目的修订版本和历史的信息. Git维护两个主要的数据结构: 对象库(object store)-在复制操作时能进行 ...

  3. jQuery可自动播放动画的焦点图

    在线演示 本地下载

  4. Refseq,accssion #,gi ,Ensembl的关系

    accession编号的分子类型代号: Ensembl是2000年就开始开发的基因组自动注释软件,起初是只对真核生物基因组,2009年后开始对植物,细菌等开放.既然要注释,就要有注释对象(基因,转录本 ...

  5. 2062326 齐力锋 实验一《Java开发环境的熟悉》实验报告

    北京电子科技学院(BESTI) 实     验    报     告 课程:   程序设计与数据结构           班级:      1623           姓名:  齐力锋      学 ...

  6. java/kotlin 读取文件、写入文件

    package dh.btb.backend.utils import java.io.*object FileUtil { /** * 创建文件 * @param filePath 文件路径(不要以 ...

  7. eclipse显示结果窗口字体大小

    设置前的字体大小 设置后的字体大小 步骤

  8. Java中Collections.sort()排序详解

      public static void main(String[] args) { List<String> list = new ArrayList<String>(); ...

  9. TCP异步IO_服务端_测试

    1.测试代码来自于 JDK7 AIO初体验 http://www.iteye.com/topic/1113611 1.1. package aio; import java.net.InetSocke ...

  10. LinkedBlockingQueue 与ConcurrentLinkedQueue队列的不同与同

    LinkedBlockingQueue 的API中,从队列中获取元素,有以下几个方法: 1.take():原文:Retrieves and removes the head of this queue ...