题目描述:

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. springboot的Scheduled定时器不工作

    问题情况 使用springboot,使用注解方式启动定时器进行业务调度. 在入口类中加了注解如下: package org.test.xyz; @SpringBootApplication @Enab ...

  2. 关于JSON字符串

    向客户端返回JSON字符串有两种方法: 1.纯手工拼接: result.append("{"); result.append("\"timu\":\& ...

  3. linux 基础知识总结

    1. 查看目录文件命令: 查看以f开头的文件:ll f* 查看/usr/local目录下的文件:ll /usr/local 按最后的修改的时间顺序,列出:ll -t */f*             ...

  4. Linux设备驱动程序加载/卸载方法 insmod和modprobe命令

    linux加载/卸载驱动有两种方法. 1.modprobe 注:在使用这个命令加载模块前先使用depmod -a命令生成modules.dep文件,该文件位于/lib/modules/$(uname ...

  5. mysql行列互相转换

    列转行: mysql> select * from test; +------+----------+-------+ | id | subject | score | +------+---- ...

  6. 20165101刘天野 2017-2018-2 《Java程序设计》 结对编程练习_四则运算(第二周)

    20165101刘天野 2017-2018-2 <Java程序设计> 结对编程练习_四则运算(第二周) 一.需求分析 能随机生成n道四则运算题目,n由使用者输入 支持分数运算 支持多运算符 ...

  7. JavaWeb 自定义标签库开发传统标签

    自定义标签主要用于移除Jsp页面中的java代码. 移除jsp页面中的java代码,只需要完成两个步骤: 编写一个实现Tag接口的Java类,并覆盖doStartTag方法,把jsp页面中的java代 ...

  8. Ctrl+Z 暂停程序及重启程序【转】

    本文转自:https://blog.csdn.net/duyiwuer2009/article/details/43191799 Ctrl+Z - 暂停进程并放入后台 jobs - 显示当前暂停的进程 ...

  9. 调用webservices中 枚举类型没有被序列化问题

    引用服务后,代理类为自动为所有枚举类型生成了一个Bool类型相关字段,命名方式:比如枚举类名为“PayType”,生成的相关字段为“PayTypeSpecified”,此字段有何作用? PayType ...

  10. Android的四种储存方式(SQLite、FileSystem、SDCardSystem、SharedPreferences)

    主要记录一下安卓中几种常用的存储方式的用法. 一.SQLite 1.创建SQLiteOpenHelper对象(当然SQLiteOpenHelper是抽象类,不能直接创建): 2.通过上面创建的对象调用 ...