636.Kth Largest Element in an Array

1.Problem

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.

题意很简单,找到一个一维数组中的第K大的数并返回。数组中第K大的数也是面试中经常考察的问题。现在就借Leetcode上的这题来详细总结下这个问题的几种解法。

2.Solution

//排序 时间复杂度为O(N*logN) 空间复杂度O(1)
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k ];
}
}
//优先队列来维护数据的有序性,超出k的部分移除出优先队列 时间复杂度O(N*logK),空间复杂度O(K)
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> p = new PriorityQueue<>();
for ( int a : nums ) {
p.offer(a); if ( p.size() > k ) {
p.poll();
}
}
return p.peek();
}
}
//快速排序思想,平均时间复杂度O(N),最坏的情况下会是O(N^2),空间复杂度为O(1)
class Solution {
public int findKthLargest(int[] nums, int k) {
int Left = 0,Right = nums.length - 1;
while (Left < Right ) {
int left = Left;
int right = Right;
int key = nums[left]; while ( left < right ) {
while ( left < right && nums[right] < key ) {
right--;
} nums[left] = nums[right]; while ( left < right && nums[left] >= key ) {
left++;
} nums[right] = nums[left];
} nums[left] = key;
if ( left == k - 1 ) {
return nums[left];
} else if ( left > k - 1 ) {
Right = left - 1;
} else {
Left = left + 1;
}
}
return nums[k - 1];
}
}

Leetcode 之 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. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

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

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

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 【leetcode】Kth Largest Element in an Array (middle)☆

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

  9. [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 ...

随机推荐

  1. JS高程3:DOM-节点层次

    DOM是一个API,通过该API可以操作HTML文档或者XML文档. DOM将HTML或者XML文档描绘成一个多层节点结构. 文档节点是HTML或者XML文档的根节点,同时也是其他节点的根节点,因为每 ...

  2. Powershell对象条件查询筛选

    在 Windows PowerShell 中,与所需的对象数量相比,通常生成的对象数量以及要传递给管道的对象数量要多得多.可以使用 Format cmdlet 来指定要显示的特定对象的属性,但这并不能 ...

  3. [转]RPC、CORBA、WebService之区别

    RPC是由Sun发明的远程过程调用协议,是第一种真正的分布式应用模型.Windows上使用的R PC是DCERPC的扩展.严格地说,RPC是一种逻辑上的协议,它可以使用Socket.Named Pip ...

  4. 谈谈哥的python爬虫书写之路

    为了做一个百度网盘搜索引擎,哥开始研究爬虫,从此迷上爬虫而一发不可收拾,现在就大概谈谈哥的爬虫之路,顺便给出引擎:http://www.quzhuanpan.com 首先基本的 Python 语法你要 ...

  5. 【Mac + Git】之git status中文文件名编码问题解决

    一.现象: 命令行输入:git status时,显示中文名乱码问题 二.解决办法: 命令行输入: git config --global core.quotepath false 通过将git配置变量 ...

  6. UFLDL深度学习笔记 (二)SoftMax 回归(矩阵化推导)

    UFLDL深度学习笔记 (二)Softmax 回归 本文为学习"UFLDL Softmax回归"的笔记与代码实现,文中略过了对代价函数求偏导的过程,本篇笔记主要补充求偏导步骤的详细 ...

  7. c# 将html添加进剪贴板(带格式)

    调用: ClipboardHelper.CopyToClipboard("<h1>hello world</h1>", ""); /// ...

  8. 说说常见的几个js疑难点

    JavaScript match() 方法 定义和使用方法 match() 方法可在字符串内检索指定的值,或找到一个或多个正則表達式的匹配. 该方法类似 indexOf() 和 lastIndexOf ...

  9. poj 1419(图的着色问题,搜索)

    题目链接:http://poj.org/problem?id=1419 思路:只怪数据太弱!直接爆搜,按顺序搜索即可. #include<iostream> #include<cst ...

  10. Ubuntu 16.04 LTS sublime text 3 解决不能输入中文

    sublime text 3 安装完成后不能输入中文,让人很是不爽.下面内容可以解决使用问题! 一.首先要注意几个问题. 1)sublime_imfix.c 文件放在home目录下面. 2)如果你在步 ...