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. C++类成员布局

    在C++中对象的内存布局与类成员声明的顺序一致,静态成员放在数据区(Data Section)而非对象内存中,若多个类静态成员名称相同,C++则按照name mangling技术进行重命名保证名称的唯 ...

  2. 提升Nginx+PHP-FPM性能技巧

    /etc/php-fpm.d 2.1进程数   php-fpm初始/空闲/最大worker进程数      pm.max_children = 300      pm.start_servers = ...

  3. The Docker学习记录[Doing]

    微服务 & Docker 译文:使用Java构建微服务 原文:Building Microservices With Java [编者的话]本文翻译自Dzone Guide to the Ja ...

  4. EntityFramework 优化

    1.分页的时候,尽量在数据库里面去分页. //在数据库中分页 ).Take().ToList(); //先把数据从数据库中查出来,然后才分页 ).Take(); 2.尽量禁用延迟加载,尽量使用预加载和 ...

  5. xmind的第十一天笔记

  6. Java中对session的简单操作

    1.jsp中操作session <% String name=(String)request.getSession().getAttribute("username"); / ...

  7. sql server 更新视图的sp

    create procedure RefreshAllViewas begin declare @ViewName varchar(250) declare #views cursor for sel ...

  8. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据

    1. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为Click 2. 实现De ...

  9. EL表达式有无双引号的区别

    最近做项目时发现原来对EL表达式理解太浅,通过一个springMVC项目,加深了对其的理解,下面总结一下,如发现有不对之处,请批评指正: 1.在单独的js文件中,EL表达式无效,如:var type= ...

  10. DBA-mysql-字符集

    查看支持的字符集:show charset; 查看现使用字符集:status; 1.在[mysqld]下添加 default-character-set=utf8 (mysql 5.1版本) char ...