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.
    • public class Solution { //桶排序
      public List<Integer> topKFrequent(int[] nums, int k) { //也可以使用PriorityQueue
      Map<Integer,Integer> map=new HashMap<Integer,Integer>();
      List<Integer> res =new ArrayList<Integer>();
      for(int num: nums){
      map.put(num,map.getOrDefault(num,0)+1);
      }
      List<Integer>[] bucket=new List[nums.length+1];
      for(int key : map.keySet()){
      int freq=map.get(key);
      if(bucket[freq]==null){
      bucket[freq]=new ArrayList<Integer>();
      }
      bucket[freq].add(key);
      }
      for(int i=nums.length;i>=0 && res.size()<k;i--){
      if(bucket[i]!=null)
      res.addAll(bucket[i]);
      }
      return res;
      }
      }

        

      Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

(Collection)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. 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 ...

  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. 347. Top K Frequent Elements (sort map)

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

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

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

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

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

  8. [LC] 347. Top K Frequent Elements

    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. js对象详解

    js自定义对象 一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 目前在J ...

  2. ubuntu随笔

    在命令行里输入 sudo nautilus 之后输入你的用户的密码,会弹出一个目录窗口来,可以复制到这来

  3. CentOS 系统安装

    1.安装时分区建立 建立/boot分区 500M /swap 10000M 剩余空间建立PV PV加入VG 建立LV 全部挂在到/ 2安装模式选择:基本安装 开发工具全部装,SERVER 全部不选,中 ...

  4. js 获取当前焦点所在的元素、给元素和input控件添加键盘监听事件、添加页面级的键盘监听事件

    页面级的键盘监听事件 document.onkeydown = function (event) { var e = event || window.event || arguments.callee ...

  5. linux的一些与关机和重启相关的命令

    runlevel 查看系统级别 cat /etc/inittab 修改系统默认运行级别 logout 退出登录

  6. tomcat 7.0 之文件配置

  7. the difference between const int *, int * const, int const *

    Some people may be confused about the sequence of const and * on declaration in C++/C, me too. Now I ...

  8. 修复PHP在64位下序列化(serialize)的字符串在32位机器下反序列

    32机器下PHP 整型数值的范围最大不超过2147483647,而有些超出范围的数值在64序列化好的数据标识为整型,在反序列时就可能会出错. 尝试使用以下的办法可以修复此问题 function int ...

  9. Cocos2d-x 3.x 错误 cocos2dxDownloader 编译报错

    因为自带的库不是自己写的代码,所以报错的当时就傻了 报错文件为Cocos2dxDownloader.java 错误铺满全屏,仔细查看发现是类的调用有问题,翻到最上边发现是有几个(很多个)类没有定义,我 ...

  10. shell 脚本技巧

    1.Give ECHO colors see see! NORMAL=$(tput sgr0) GREEN=$(tput setaf ; tput bold) YELLOW=$(tput setaf ...