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. Python 中的map和reduce学习笔记

    map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...

  2. mysql 导入导出方法。

    1.导出  mysqldump -u username - p databasename >名.sql enter passward: 2.导入:mysql -uroot -proot sour ...

  3. 布隆过滤器的概述及Python实现

    布隆过滤器 布隆过滤器是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时,能很好地取舍空间使用率与误报比例.正是由于这个特性,它被称作概 ...

  4. iOS开发之APP上线

    APP 上线有两种途径: 一种是 Xcode->openDeveloperTool->applicationLoader,这种打开后登陆appleID就可以选取并且交付您的应用程序了.这种 ...

  5. sdk 简单说明文档草稿。

    SDK初始化: HighApi为SDK核心类,请客户端持有其唯一单例对API进行调用. HighApi构造器函数 HighApi(Context appContext, final String ap ...

  6. Bootstrap模态框(MVC)

    BZ这篇博客主要是为大家介绍一下MVC如何弹出模态框.本文如果有什么不对的地方,希望大神们多多指教,也希望和我一样的小菜多多学习.BZ在这里谢过各位. 首先要在页面加上一个点击事件: @Html.Ac ...

  7. chosen组件实现下拉框

    chosen组件用于增强原生的select控件,使之有更好的用户体验.官方demo https://harvesthq.github.io/chosen/ 目前项目中碰到的使用,比如一个页面中有两个不 ...

  8. comboBox 手动输入后回车自动更新数据

    C# Winform ComboBox 在输入内容时 会在下拉菜单中显示 根据输入内容查询的结果 2014-01-02 16:42匿名 | 浏览 713 次 C# ComboBox 在输入内容时 会在 ...

  9. RestEasy 3.x 系列之四:使用Hibernate_Validator进行数据校验

    使用Hibernate_Validator进行数据校验,好处不言而喻:规范统一,低耦合度. 1.pom.xml <dependency> <groupId>org.hibern ...

  10. NSSM - windows 服务安装工具

    nssm  windows 服务安装工具,简单方便, windows service wrapper 也是一个类似的工具,但是需要进行配置文件编写= 下载的地址: http://nssm.cc/rel ...