11/10 <priorityQueue> 215 347
215. Kth Largest Element in an Array
快速排序法,选择一个数,比这个数大的交换到左边,比这个数小的交换到右边。
- class Solution {
- public int findKthLargest(int[] nums, int k) {
- shuffle(nums);
- k = nums.length - k;
- int lo = 0, hi = nums.length - 1;
- while(lo < hi){
- final int j = partition(nums, lo, hi);
- if(j < k){
- lo = j + 1;
- }else if( j > k){
- hi = j - 1;
- }else{
- break;
- }
- }
- return nums[k];
- }
- public int partition(int[] a, int lo, int hi){
- int i = 0;
- int j = hi + 1;
- while(true){
- while(i < hi && less(a[++i], a[lo]));
- while(j > lo && less(a[lo], a[--j]));
- if(i >= j){
- break;
- }
- swap(a, i, j);
- }
- swap(a, lo, j);
- return j;
- }
- public void swap(int[] a, int i, int j){
- int tmp = a[i];
- a[i] = a[j];
- a[j] =tmp;
- }
- private void shuffle(int a[]) {
- final Random random = new Random();
- for(int ind = 1; ind < a.length; ind++) {
- final int r = random.nextInt(ind + 1);
- swap(a, ind, r);
- }
- }
- public boolean less(int v, int m){
- return v < m;
- }
- }
347. Top K Frequent Elements
用桶排序原则,bucket[]的下标表示出现的次数。用HashMap放入数字和出现的次数并放入对应的bucket中,最后从后向前取出。
HashMap.getOrDefault(n,0), 返回Key对应的Value,没有则返回0
HashMap.keySet(): 获取所有 Key
- class Solution {
- public List<Integer> topKFrequent(int[] nums, int k) {
- List<Integer>[] bucket = new List[nums.length + 1];
- Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();
- for(int n : nums){
- frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1);
- }
- for(int key : frequencyMap.keySet()){
- int frequency = frequencyMap.get(key);
- if(bucket[frequency] == null){
- bucket[frequency] = new ArrayList<>();
- }
- bucket[frequency].add(key);
- }
- List<Integer> res = new ArrayList<>();
- for(int pos = bucket.length - 1; pos >= 0 && res.size() < k; pos--){
- if(bucket[pos] != null){
- res.addAll(bucket[pos]);
- }
- }
- return res;
- }
- }
11/10 <priorityQueue> 215 347的更多相关文章
- 微信iphone7、 ios10播放视频解决方案 2016.11.10
2016.11.10日更新以下方法 微信最新出同层播放规范 即使是官方的也无法解决所有android手机的问题. 另外iphone 5 .5s 某些手机始终会弹出播放,请继续采用 “以下是老的解决办法 ...
- ubuntu 11.10 安装apache2 tomcat6
ubuntu 11.10 安装apache2 tomcat6 导读 Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目 ...
- 【转】ubuntu 11.10(32位系统)下编译android源码
原文网址:http://www.cnblogs.com/dwayne/archive/2011/11/16/2251734.html 本文介绍在ubuntu 11.10系统下编译android 2.3 ...
- Ubuntu 11.10 安装GMONE3,卸载 UNITY和UNITY 2D
Ubuntu 11.10安装GNOME3: 1)sudo apt-get install gnome-shell sudo apt-get install gnome-themes* (或者 ...
- 在Ubuntu 11.10工具栏上用数字显示网速、CPU负荷和内存占用量『译』
基本上照抄了<How To Display Network Upload / Download Speed On The Panel In Ubuntu 11.04>,只不过我的实践环境是 ...
- Ubuntu 11.10 Server下搭建Maven私服
安装Nexus服务的文档可以参考官方站点:http://www.sonatype.com/books/nexus-book/reference/install-sect-install.html ...
- Ubuntu 11.10下GRUB 2 1.99版编译安装笔记
Ubuntu 11.10下GRUB 2 1.99版编译安装笔记 以下的安装笔记,都是QLi自己学习grub2 时,所整理的,还是新手,有错误的话,请大家帮忙就别提出来了. 最新版grub V1.99官 ...
- 显示 Ubuntu 11.10 的 终端窗口
显示 Ubuntu 11.10 的 终端窗口 一.点击左上角的图标 -> 在search框里搜索termial . 二.快捷键:Ctrl+Alt+t.
- 下面程序的输出结果是____ 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) ...
随机推荐
- ASP.NET MVC https全局配置
//https全局配置 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.S ...
- 大话设计模式Python实现-适配器模式
适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口. 下面是一个适配器模式的demo: #!/usr/bin/env python # -*- coding:u ...
- JeeSite | 访问控制权限
在各种后台系统中都会涉及到权限的管控,从功能权限的管控,到数据权限的管控,都是为了让系统的在使用的过程中更加的安全.功能权限管控是对针对不同的角色可以进行不同的功能操作,而数据权限管控是针对不同的角色 ...
- tf.slice()
原文连接:https://www.jianshu.com/p/71e6ef6c121b tf.slice()到底要怎么切呢?下面通过列子来看看 方程的signature是这样的: def slice( ...
- php imagemagick 翻译目录
图像处理(ImageMagick) 介绍 安装/配置 要求 安装 运行时配置 资源类型 预定义常数 例子 基本用法 Imagick - Imagick课 Imagick :: adaptiveBlur ...
- 修改VisualStudio的智能提示字体大小
最近换了一个高分辨率显示器,便觉得VisualStudio的字体过小了,虽然VisualStudio换字体还比较简单,大部分位置的字体基本上很顺利的就换掉了,然而一直找不到智能提示的字体所在位置,放狗 ...
- Enum.GetUnderlyingType(obj.GetType())
Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:
- WPF-带有GridView的ListView样式
ListView是展示数据的常用控件,这里简单对带有GridView的ListView样式进行设置. <Style TargetType="{x:Type ListViewItem}& ...
- 记netmvc中Html.BeginForm的一个大坑
在asp.net mvc中,很常使用using(Html.BeginForm()){}来生成表单提交 不传入参数时,默认提交到原始url 最坑的是,此表单自动提交时,会将所在页面的原始url的参数也一 ...
- Python基础16
反复练习决策树案例(保险) 将老师的思路与解题过程, 自己的心得, 中间遇到的问题.陷阱.解决办法, 写出来. 总之,将这个案例消化成自己的东西!