215 Kth Largest Element in an Array 快排

题目:在无序的数组中找到第k大的元素,也就是若长度为n的数组从小到大排列时,下标为n-k的元素。
注意Example2:第4大的元素是4,也就是数组中出现的两个5分别是第2大和第3大的数字。
解法一:直接利用sort函数排序后,取第k大的元素。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end(),greater<int>());
return nums[k-];
}
};
解法二:快排
时间复杂度是:O(n)

注意几个问题:
1)k的取值:是不变的啊! 不会因为递归时在privot的左边或者右边寻找而改变,因为nums的长度n是不变的,元素的下标也是不变的;
2)partition函数递归调用自己时是要加return的!不然会报错。
class Solution {
public:
int partition(vector<int>& nums, int k, int l, int r){
int n = nums.size();
swap(nums[l],nums[rand()%(r-l+)+l]); //生成随机数
int p = nums[l];
//nums[l+1...i) <=v ; nums(j...r] >=v i和j 是需要考察的元素,所以是开区间
int i = l+, j = r; //初始定义区间为空
while(true){
while(i<=r && nums[i]<p)
i++;
while(j>=l+ && nums[j]>p)
j--;
if(i>j) break;
swap(nums[i], nums[j]);
i++;
j--;
}
swap(nums[l], nums[j]);
if(j == n-k) return nums[j];
else if(j < n-k) return partition(nums, k, j+,r);
else return partition(nums, k, l, j-);
}
int findKthLargest(vector<int>& nums, int k) {
srand(time(NULL));
int n = nums.size();
return partition(nums, k, , n-);
}
};
终于把快排思想的写对了,前一天晚上一直在报错,因为上面提到的两个点没有想明白,还是早上效率高 :)
解法二:思路是将比privot大的元素放到privot左边, 小的放到privot右边。
class Solution {
public:
int quick_sort(vector<int>& nums, int k, int left, int right){
swap(nums[left], nums[left + rand()%(right-left+)]); //随机取在nums中取一个数 与 nums[left]交换位置
int p = nums[left];
int i = left+, j = right;
while(){
while( i<=right && nums[i] >= p) // p = nums[left]
i++;
while( j>=left+ && nums[j] <= p)
j--;
if(i>=j)
break;
swap(nums[i], nums[j]);
i++;
j--;
}
// swap(nums[left], nums[j]);
swap(p, nums[j]); // 若交换p和nums[j] 因为还需要把 p 的值再赋给nums[left]
nums[left] = p;
if(j == k-)
return nums[j];
else if(k- > j)
// k-1为4,j为3时
// 第4大的数 < 第3大的数 数组是从大到小排序 故从右半部分寻
return quick_sort(nums, k, j+, right);
else
return quick_sort(nums, k, left, j-);
}
int findKthLargest(vector<int>& nums, int k) {
return quick_sort(nums, k, , nums.size()-);
}
};
215 Kth Largest Element in an Array 快排的更多相关文章
- 网易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 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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 ...
- 【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 ...
- 【刷题-LeetCode】215. Kth Largest Element in an Array
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- [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 ...
- 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 ...
- 215. Kth Largest Element in an Array(QuickSort)
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素
[抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...
- 17.SQL 约束
约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). 我们将主要探讨以下几种约束: NOT ...
- 自定义JTabbedPane 标签风格
自定义JTabbedPane 标签风格 2012年03月09日 20:33:12 阅读数:2435 demo 下载地址:http://download.csdn.net/detail/jinannan ...
- 如果你的资源贫乏,那么专注做好一件事将是你的唯一出路(no reading yet)
http://www.jianshu.com/p/8784f0fd7ab8/comments/1161511
- Python基础入门-列表解析式
今天我们使用Python中的列表解析式来实现一些简单功能.好了关于列表解析式是什么?我的理解是它可以根据已有列表,高效创建新列表的方式.列表解析是Python迭代机制的一种应用,它常用于实现创建新的列 ...
- jquery数组拼接
var a=[]; var c=[80,90,70,100] var b={'张三':19,'成绩':c}; a.push(b); console.log("测试案例",a); 同 ...
- 编写高质量代码改善C#程序的157个建议——建议43:让接口中的泛型参数支持协变
建议43:让接口中的泛型参数支持协变 除了上一建议中提到的使用泛型参数兼容接口不可变性外,还有一种办法是为接口中的泛型声明加上out关键字来支持协变,如下所示: interface ISalary&l ...
- C# 筛选Datatable、foreach填充DGV,datatable与datarows转换
public void SelectFG1(string Nane) { string SetText = "卫材代码 like '%" + Nane + "%' or ...
- 842. Split Array into Fibonacci Sequence
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- Web渗透测试(xss漏洞)
Xss介绍—— XSS (cross-site script) 跨站脚本自1996年诞生以来,一直被OWASP(open web application security project) 评为十大安 ...