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 ...
随机推荐
- vim 的移动
越来也喜欢用linux的vim 来编程了,简单.高效.专业,最近拿着一本<vim的中文使用手册>在慢慢的看,看到现在就没有勇气继续看下去,我想先放一下,运用前面自己学的东西实际的去操作一下 ...
- 图解KMP算法
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
通过 正则表达式 来获取一个网页中的所有的 URL链接,并下载这些 URL链接 的源代码 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 ...
- Linux编译提速
一.简介 项目越来越大,重新编译整个项目是一件很费时的事,总结可以帮助提速方法,如下: 1)tmpfs: 解决IO瓶颈,充分利用本机内存资源 2)make -j: 充分利用本机计算资源 3)distc ...
- html页面的局部刷新
有时候我们在做一个动态/静态网页,网页中的某部分需要从服务器获取值但是不能把整个页面都提交到服务器,也就是要对页面做局部刷新,也就是对整个网页无刷新更新值.在这种情况下就需要用JS和XMLHttpRe ...
- 前端基础 之 Bootstrap框架
浏览目录 Bootstrap介绍 为什么要使用Bootstrap? Bootstrap环境搭建 布局容器 栅格系统 Bootstrap全局样式 一.Bootstrap介绍 Bootstrap是Twit ...
- rpm遇到的坑-与VMP冲突
rpm遇到的坑-与VMP冲突 摘自:https://blog.csdn.net/shijichao2/article/details/78797586 2017年12月13日 22:29:21 阅读数 ...
- Join导致冗余数据引起慢SQL
业务过程中碰到多个join引起慢SQL问题,数据量不大,但查询很慢,搜到一片BLog,参考解决. 业务过程不记录,以blog内容重现: 原SQL: select distinct abc.pro_co ...
- 史融资2.5亿的“自主国产”红芯浏览器,其实是个套壳Chrome
红芯浏览器 今天早上看到朋友发的浏览器图片,感觉很好奇,然后就看了下,感觉文章还不错,就转发了下,然后下载浏览器着实花了不小心思,最后文末添加了红芯浏览器转存在蓝奏云盘的下载连接了. 文章原文 今天又 ...
- 编写高质量代码改善C#程序的157个建议——建议7: 将0值作为枚举的默认值
建议7: 将0值作为枚举的默认值 允许使用的枚举类型有byte.sbyte.short.ushort.int.uint.long和ulong.应该始终将0值作为枚举类型的默认值.不过,这样做不是因为允 ...