215. Kth Largest Element in an Array

快速排序法,选择一个数,比这个数大的交换到左边,比这个数小的交换到右边。

  1. class Solution {
  2. public int findKthLargest(int[] nums, int k) {
  3. shuffle(nums);
  4. k = nums.length - k;
  5. int lo = 0, hi = nums.length - 1;
  6. while(lo < hi){
  7. final int j = partition(nums, lo, hi);
  8. if(j < k){
  9. lo = j + 1;
  10. }else if( j > k){
  11. hi = j - 1;
  12. }else{
  13. break;
  14. }
  15. }
  16. return nums[k];
  17. }
  18.  
  19. public int partition(int[] a, int lo, int hi){
  20. int i = 0;
  21. int j = hi + 1;
  22. while(true){
  23. while(i < hi && less(a[++i], a[lo]));
  24. while(j > lo && less(a[lo], a[--j]));
  25. if(i >= j){
  26. break;
  27. }
  28. swap(a, i, j);
  29. }
  30. swap(a, lo, j);
  31. return j;
  32. }
  33.  
  34. public void swap(int[] a, int i, int j){
  35. int tmp = a[i];
  36. a[i] = a[j];
  37. a[j] =tmp;
  38. }
  39.  
  40. private void shuffle(int a[]) {
  41.  
  42. final Random random = new Random();
  43. for(int ind = 1; ind < a.length; ind++) {
  44. final int r = random.nextInt(ind + 1);
  45. swap(a, ind, r);
  46. }
  47. }
  48.  
  49. public boolean less(int v, int m){
  50. return v < m;
  51. }
  52. }

347. Top K Frequent Elements

用桶排序原则,bucket[]的下标表示出现的次数。用HashMap放入数字和出现的次数并放入对应的bucket中,最后从后向前取出。

HashMap.getOrDefault(n,0), 返回Key对应的Value,没有则返回0

HashMap.keySet(): 获取所有 Key

  1. class Solution {
  2. public List<Integer> topKFrequent(int[] nums, int k) {
  3. List<Integer>[] bucket = new List[nums.length + 1];
  4. Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();
  5.  
  6. for(int n : nums){
  7. frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1);
  8. }
  9.  
  10. for(int key : frequencyMap.keySet()){
  11. int frequency = frequencyMap.get(key);
  12. if(bucket[frequency] == null){
  13. bucket[frequency] = new ArrayList<>();
  14. }
  15. bucket[frequency].add(key);
  16. }
  17.  
  18. List<Integer> res = new ArrayList<>();
  19. for(int pos = bucket.length - 1; pos >= 0 && res.size() < k; pos--){
  20. if(bucket[pos] != null){
  21. res.addAll(bucket[pos]);
  22. }
  23. }
  24. return res;
  25. }
  26. }

11/10 <priorityQueue> 215 347的更多相关文章

  1. 微信iphone7、 ios10播放视频解决方案 2016.11.10

    2016.11.10日更新以下方法 微信最新出同层播放规范 即使是官方的也无法解决所有android手机的问题. 另外iphone 5 .5s 某些手机始终会弹出播放,请继续采用 “以下是老的解决办法 ...

  2. ubuntu 11.10 安装apache2 tomcat6

    ubuntu 11.10 安装apache2 tomcat6 导读 Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目 ...

  3. 【转】ubuntu 11.10(32位系统)下编译android源码

    原文网址:http://www.cnblogs.com/dwayne/archive/2011/11/16/2251734.html 本文介绍在ubuntu 11.10系统下编译android 2.3 ...

  4. Ubuntu 11.10 安装GMONE3,卸载 UNITY和UNITY 2D

    Ubuntu 11.10安装GNOME3: 1)sudo apt-get install gnome-shell    sudo apt-get install gnome-themes*   (或者 ...

  5. 在Ubuntu 11.10工具栏上用数字显示网速、CPU负荷和内存占用量『译』

    基本上照抄了<How To Display Network Upload / Download Speed On The Panel In Ubuntu 11.04>,只不过我的实践环境是 ...

  6. Ubuntu 11.10 Server下搭建Maven私服

      安装Nexus服务的文档可以参考官方站点:http://www.sonatype.com/books/nexus-book/reference/install-sect-install.html ...

  7. Ubuntu 11.10下GRUB 2 1.99版编译安装笔记

    Ubuntu 11.10下GRUB 2 1.99版编译安装笔记 以下的安装笔记,都是QLi自己学习grub2 时,所整理的,还是新手,有错误的话,请大家帮忙就别提出来了. 最新版grub V1.99官 ...

  8. 显示 Ubuntu 11.10 的 终端窗口

    显示 Ubuntu 11.10 的 终端窗口 一.点击左上角的图标 -> 在search框里搜索termial . 二.快捷键:Ctrl+Alt+t.

  9. 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);

    下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...

随机推荐

  1. ASP.NET MVC https全局配置

    //https全局配置 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.S ...

  2. 大话设计模式Python实现-适配器模式

    适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口. 下面是一个适配器模式的demo: #!/usr/bin/env python # -*- coding:u ...

  3. JeeSite | 访问控制权限

    在各种后台系统中都会涉及到权限的管控,从功能权限的管控,到数据权限的管控,都是为了让系统的在使用的过程中更加的安全.功能权限管控是对针对不同的角色可以进行不同的功能操作,而数据权限管控是针对不同的角色 ...

  4. tf.slice()

    原文连接:https://www.jianshu.com/p/71e6ef6c121b tf.slice()到底要怎么切呢?下面通过列子来看看 方程的signature是这样的: def slice( ...

  5. php imagemagick 翻译目录

    图像处理(ImageMagick) 介绍 安装/配置 要求 安装 运行时配置 资源类型 预定义常数 例子 基本用法 Imagick - Imagick课 Imagick :: adaptiveBlur ...

  6. 修改VisualStudio的智能提示字体大小

    最近换了一个高分辨率显示器,便觉得VisualStudio的字体过小了,虽然VisualStudio换字体还比较简单,大部分位置的字体基本上很顺利的就换掉了,然而一直找不到智能提示的字体所在位置,放狗 ...

  7. Enum.GetUnderlyingType(obj.GetType())

    Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:

  8. WPF-带有GridView的ListView样式

    ListView是展示数据的常用控件,这里简单对带有GridView的ListView样式进行设置. <Style TargetType="{x:Type ListViewItem}& ...

  9. 记netmvc中Html.BeginForm的一个大坑

    在asp.net mvc中,很常使用using(Html.BeginForm()){}来生成表单提交 不传入参数时,默认提交到原始url 最坑的是,此表单自动提交时,会将所在页面的原始url的参数也一 ...

  10. Python基础16

    反复练习决策树案例(保险) 将老师的思路与解题过程, 自己的心得, 中间遇到的问题.陷阱.解决办法, 写出来. 总之,将这个案例消化成自己的东西!