Leetcode 之 Kth Largest Element in an Array
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的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 【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 ...
- [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 ...
随机推荐
- layui基础上的tree菜单动态渲染;
var layout=[ { title:'脚本对象名称', treeNodes:true, headerClass:'value_col', colClass:'value_col', style: ...
- redis 服务相关
一 什么是redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开 ...
- CentOs 7 安装总结
1.安装 前几天安装了CentOs 7系统,弄了好几次都进不去图形安装界面,总是进入 "dracut:/# _".本以为是系统盘没做好,就用了另外几种工具重做了盘,还是一样.后来在 ...
- strncmp----c++库函数
某天,蒜头君和花椰妹在公园里散步,走着走着,我的天!他们各自都捡到了一串漂亮的字符串,然而蒜头君好奇心比较重,他想知道自己的字符串在花椰妹的字符串中出现了多少次,例如花椰妹的字符串为abababa,蒜 ...
- 堆积木----vector防止内存超限
蒜头君有 nn 块积木,编号分别为 11 到 nn.一开始,蒜头把第 ii 块积木放在位置 ii.蒜头君进行 mm 次操作,每次操作,蒜头把位置 bb 上的积木整体移动到位置 aa 上面.比如 11 ...
- Linux 下使用静态google protocl buffer
在Linux上编译google protobuff时,configure 默认选项是生成动态库,即libprotobuf.so文件.如果同时在多个动态库(动态库以dlopen方式动态加载)中使用同一b ...
- visitor设计模式记录
数据类型通过枚举来区分是一种简单实用的做法. 缺点是使用的时候需要通过if .switch 去判断什么类型执行什么分支操作,说是缺点其实也要看具体场景.不过如果if代码多会导致代码很长是肯定的. 复杂 ...
- 为什么 MongoDB 连接数被用满了?
使用 MongoDB 时,可能会遇到因为 mongod 连接数用满了,导致客户端无法连接的问题.mongod的最大连接数通过 net.maxIncomingConnections 指定,默认值为100 ...
- Origin8.0使用心得(不定时更新)
如何画双Y轴坐标? 网上的教程很多,有点鱼龙混杂的感觉,今天整理一下最简单的方法,备忘. 有3组数据,1组自变量,2组因变量.如何在同一张图下使用同一个X轴,但左边的Y轴与右边的Y轴的刻度不一样? 最 ...
- VisualSVN 5.1.5 破解版 手动破解教程 生成dll文件
VisualSVN 5.1.5 破解版 手动破解教程 生成VisualSVN.Core.L.dll文件 附上本人用到的命令: ildasm "D:\Program Files (x86)\V ...