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 ...
随机推荐
- 谈一谈APP版本号问题
如题:谈一谈APP版本号问题 为什么要谈这个问题,周五晚上11~12点,被微信点名,说APP有错,无效的版本号,商城无法下单.我正在准备收拾东西,周末回老家,结果看到这样问题,菊花一紧.我擦,我刚加的 ...
- Jquery学习笔记(11)--jquery的ajax删除用户,非常简单!
jquery的ajax,简直简单!!只要一个$.get(url,map,function)就搞定了! index.php: <!DOCTYPE html> <html lang=&q ...
- The Definitive Guide To Django 2 学习笔记(二) 第二个View 动态内容
“Hello World”只是简单的展现了Django 基本的工作机制,但它不是动态的网页.第二个View我们将创建一个动态的网页,该页面上将展现当前的时间和日期. 该View需要做两件事,第一,计算 ...
- CRM认识的误区
众所周知,CRM,就是平时说的“客户关系管理”,指用CRM来管理 企业与客户之间的关系.纵观整个IT圈子,做CRM的厂商比比皆是,每个厂商都有自己的产品宣言,令人眼花缭乱.但是领很多老板们不解的是,我 ...
- 解决windows server 2008 r2 右键没有个性化
装完系统发现桌面上只有一个回收站我的电脑 网络邻居 控制板面都没有好像win7家庭版也没有右键–个性化设置上google百度了下 也还漫简单的点左下角的开始-运行:"C:\Windows\s ...
- hdu6055 Regular polygon 脑洞几何 给定n个坐标(x,y)。x,y都是整数,求有多少个正多边形。因为点都是整数点,所以只可能是正四边形。
/** 题目:hdu6055 Regular polygon 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055 题意:给定n个坐标(x,y).x,y都 ...
- [转]Linux系统下yum和apt-get的区别
一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...
- SpringBoot 获取前端页面参数的集中方式总结
SpringBoot的一个好处就是通过注解可以轻松获取前端页面的参数,之后尅将参数经过一系列处理传送到后台数据库,前端时间正好用到.大致分为一下几种: 1.指定前端URL请求参数名称与方法名称一致,这 ...
- asp.net知识汇总-页面跳转Server.Transfer和Response.Redirect
1. Server.Transfer 服务器端跳转 webform1.aspx跳转到webform2.aspx页面 webform1.aspx代码如下: protected void Page_Loa ...
- 【C++自我精讲】基础系列三 重载
[C++自我精讲]基础系列三 重载 0 前言 分二部分:函数重载,操作符重载. 1 函数重载 函数重载:指在同一名字空间中,函数名称相同,参数类型.顺序或数量不同的一类函数,同一函数名的函数能完成不同 ...