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 ...
随机推荐
- Mac notes
1. Mac应用数据存放位置 ~/Library/Application Support/ 比如sublime text的应用数据~/Library/Application Support/Subli ...
- django: django rest framework 分页
django: django rest framework 分页 2018年06月22日 13:41:43 linux_player_c 阅读数:665更多 所属专栏: django 实战 版权声 ...
- 复习扩展方法 涉及委托,这里我使用自定义委托类型 public delegate bb MyFunc<in T,out bb> (T arg)
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...
- Part3_lesson4---协处理器访问指令
1.什么是协处理器? CP15是协处理器, CP15的作用:系统控制协处理器CP15,它提供了额外的寄存器,这些寄存器用于配置和控制cache,MMU,保护系统,时钟模式,和其他的系统项,比如大小端操 ...
- 远程访问Function时报错Remote table-valued function calls are not allowed.
开始是这样调用的:select * from [LinkedServer].[db name].dbo.[function name](param1, param2) 原因: Only table-v ...
- Open Message Queue 集群问题
nohup ./imqbrokerd -tty -name myBroker -port 7677 -javahome /opt/omae/jdk1.7.0_45 -cluster 192.168.2 ...
- Cookie的跨域问题
被误解的HttpCookie.Domain属性 有人说可以利用HttpCookie.Domain属性实现跨域访问,假如在A站(A.com)中写B站(B.com)的cookie,如下所示 这其实是错误的 ...
- URAL 1748. The Most Complex Number(反素数)
题目链接 题意 :给你一个n,让你找出小于等于n的数中因子个数最多的那个数,并且输出因子个数,如果有多个答案,输出数最小的那个 思路 : 官方题解 : (1)此题最容易想到的是穷举,但是肯定超时. ( ...
- C++语法知识小结(持续更新中)
1)在适用构造函数创建对象时,有时会创建临时对象.如 Stock::Stock(const std::string & co,long n,double pr); 在使用时,下面两条语句有根本 ...
- JAVA的String 类【转】
String类 1.String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下: String s = “abc”; s ...