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 ...
随机推荐
- 写一个简单的form表单,当光标离开表单的时候表单的值发送给后台
<body> <form action="index.php"> <input type="text" name="tx ...
- mysql唯一主键生成代码
<insert id="insertPeople" parameterType="PeopleSchedule"> <selectKe ...
- [转]Spark能否取代Hadoop?
大数据的浪潮风靡全球的时候,Spark火了.在国外 Yahoo!.Twitter.Intel.Amazon.Cloudera 等公司率先应用并推广 Spark 技术,在国内阿里巴巴.百度.淘宝.腾讯. ...
- java项目的部署
1.将tomocat解压到服务器上 2.放项目war包. 3.war包解压. 4.修改端口配置. 1.<Server port="8024" shutdown="S ...
- Oracle常用命令大全
一.ORACLE的启动和关闭 1.在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a.启动ORACLE系统 oracle>svrmgrl ...
- 【理财】阅读:Millionaire Teacher
书是本好书,就是翻译的中文书名让人无力吐槽了,叫<拿工薪,三十几岁你也能赚到600万>,浓浓的畅销书架味道有木有. 这本书作者极力推崇指数基金,以及全球极简投资配置策略.重点梳理了一下: ...
- html 调用ActiveX
html网页调用ActiveX控件时,要获取到ActiveX的ClassID,这个ClassID是注册到系统里的,而不是工程中的uuid,(下图为uuid). 正确的是在注册表的HKEY_CLASSE ...
- c#上传大文件方法
客户端代码: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param n ...
- Dependency Property 依赖属性
依赖属性就是一种可以自己没有值,并能通过使用Binding从数据源获得值(依赖在别人身上)的属性.拥有依赖属性的对象称为“依赖对象”. WPF开发中,必须使用依赖对象作为依赖属性的宿主,使二者结合起来 ...
- Numpy常用金融计算(一)
In [41]: a=[1,2,3,4,5,5,6,6,7,8,8,9,9] # list类型数组 In [42]: b=nu.mean(a) #调用numpy.mean方法计算数组元素的算术平均值 ...