-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76.findKthLargest.cpp
More file actions
executable file
·74 lines (68 loc) · 1.6 KB
/
Copy path76.findKthLargest.cpp
File metadata and controls
executable file
·74 lines (68 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
@filename 76.findKthLargest.cpp
@author caonan
@date 2022-05-11 16:20:32
@reference 剑指offer专项
@url https://leetcode.cn/problems/xx4gT2/
@brief 给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。
请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "../util/util.h"
using namespace std;
//用快排的思路去做
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int start = 0;
int end = nums.size() - 1;
int ipos = nums.size() - k;
while (true) {
int pos = partition(start, end, nums);
if (pos == ipos) {
break;
}
if (pos < ipos) {
start = pos + 1;
} else {
end = pos - 1;
}
}
return nums[ipos];
}
private:
int partition(int start, int end, vector<int>& nums) {
int random = start;
std::swap(nums[random], nums[end]);
int p1 = start - 1;
for (int p2 = start; p2 < end; p2++) {
if (nums[p2] < nums[end]) {
p1++;
std::swap(nums[p1], nums[p2]);
}
}
p1++;
std::swap(nums[p1], nums[end]);
return p1;
}
};
int main() {
Solution s;
vector<int> in{3, 2, 1, 5, 6, 4};
int ret = s.findKthLargest(in, 2);
cout << "ret is " << ret << endl;
return 0;
}