-
Notifications
You must be signed in to change notification settings - Fork 0
/
215.数组中的第k个最大元素.cpp
49 lines (45 loc) · 963 Bytes
/
215.数组中的第k个最大元素.cpp
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
/*
* @lc app=leetcode.cn id=215 lang=cpp
*
* [215] 数组中的第K个最大元素
*/
// @lc code=start
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int findKthLargest(vector<int>& nums, int k)
{
vector<int> ge, eq, le;
int pivot = nums[0];
for (int num : nums)
{
if (num < pivot)
{
le.push_back(num);
}
if (num == pivot)
{
eq.push_back(num);
}
if (num > pivot)
{
ge.push_back(num);
}
}
if (k <= ge.size())
{
return findKthLargest(ge, k);
}
if (k > ge.size() && k <= ge.size() + eq.size())
{
return pivot;
}
else
{
return findKthLargest(le, k - ge.size() - eq.size());
}
}
};
// @lc code=end