Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

题意:

给出一个未排序的数组,找出数组中第K大的元素。

方法:

使用优先级队列来解决。

优先级队列是不同于先进先出队列的另一种队列,每次从队列中取出的是具有最高优先权的元素。

如果不是提供comparator的话,优先队列中的元素默认按照自然顺序排列,

也就是数字小的默认在队列头,字符串按照字典顺序排列。

public class Solution {
//优先队列,
//遍历数组时将数字加入优先队列(堆),一旦堆的大小大于k就将堆顶元素去除,
//确保堆的大小为k,
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> p = new PriorityQueue<Integer>();
for(int i=0; i<nums.length;i++){
p.add(nums[i]);
if(p.size()>k) p.poll();
}
return p.poll(); }
}

leetcode 215. Kth Largest Element in an Array的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  3. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  4. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. Java for LeetCode 215 Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  6. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

    题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...

  8. C#版 - Leetcode 215. Kth Largest Element in an Array-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

随机推荐

  1. json的解释

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人阅读和编写,同时也易 ...

  2. qt5.4

    rm -f libQt5Qml.so.5.4.0 libQt5Qml.so libQt5Qml.so.5 libQt5Qml.so.5.4g++ -Wl,-O1,--sort-common,--as- ...

  3. 创建 sequence

    -- Create sequence create sequence XRMKSD_DET_SEQminvalue 1maxvalue 999999999999999999999999999start ...

  4. Java包装类

    自动装箱:指开发人员可以把一个基本数据类型直接赋给对应的包装类 自动拆箱:指开发人员可以把一个包装类对象直接赋给对应的基本数据类型 要把基本数据类型称为对象的时候,需要把基本数据类型进行包装, 运用: ...

  5. JavaScript学习笔记——基本知识

    JavaScript学习的教程来自后盾网 1>JavaScript的放置和注释 1.输出工具 A.alert(); B.document.write(); C.prompt("&quo ...

  6. ecshop后台【订单管理】

    1.订单列表页,在‘确认’,‘无效’,’取消‘....增加一个选项’导出exl表格‘ a.增加html代码,order_list.htm <input name="print" ...

  7. nuget包管理器控制台下的powershell脚本介绍

    http://personball.com/powershell/2016/07/15/powershell-tips 定制自己的powershell,减少重复工作 安装一系列自己的常用nuget包 ...

  8. thinkphp 3.2.3 连接sql server 2014 WAMPSERVER环境包

    安装 sqlsrv 扩展 首先  sql server 2014 安装没啥说的 链接信息自己设置 php 版本 :5.5.12 sqlsrv 驱动  微软提供了 3.0 和3.1 版本  3.0 对应 ...

  9. 网络方案 & HTTP状态码

    在iOS中,常见的发送HTTP请求的方案包括: 苹果官方 名称 说明 NSURLConnection iOS 2.0 推出,用法简单,最古老最经典最直接的一种方案 NSURLSession iOS 7 ...

  10. iOS- storyboard this class is not key value coding-compliant for the key xxx

    如图: 在使用storyboard的时候出现此问题,主要是因为给storybroad中的view拖线的时候,有时不小心线拖错了,或者再次拖线导致代码中控件的名字与之前拖线时定义的名字不同导致的. 解决 ...