-
Notifications
You must be signed in to change notification settings - Fork 0
/
最小的K个数.txt
100 lines (99 loc) · 2.85 KB
/
最小的K个数.txt
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**************************************************************
利用快排的思想找到前K个最小的数字,复杂度为O(n)
**************************************************************
class Solution {
public:
void Swap(int &x,int &y)
{
int temp = x;
x=y;
y=temp;
}
int Partition(vector<int> &numbers, int length, int start, int end)
{
if(numbers.size()<1 || length < 0 || start < 0 || end >= length)
return 0;
int index = rand()%(end-start+1)+start;
Swap(numbers[index],numbers[end]);
int small = start -1;
for(index = start;index < end; ++index)
if(numbers[index] < numbers[end])
{
++ small;
if(small != index)
Swap(numbers[index],numbers[small]);
}
++small;
Swap(numbers[small],numbers[end]);
return small;
}
vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
{
vector<int> output;
int length = input.size();
if(length < 1 || k <=0 || k > length)
return output;
int start = 0;
int end = length -1;
int index = Partition(input,length,start,end);
while(index != k-1)
{
if(index > k-1)
{
end = index - 1;
index = Partition(input,length,start,end);
}
else
{
start = index + 1;
index = Partition(input,length,start,end);
}
}
for(int i = 0; i < k; ++i)
output.push_back(input[i]);
return output;
}
};*/
/*******************************************************
利用长度为K的最大堆实现O(nlog(k))
********************************************************/
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
{
maxHeap(input,k);
vector<int> result;
if(intSet.size()==0)
return result;
setIterator = intSet.end();
setIterator--;
for(int i = 0;i < k;i++,setIterator--)
{
result.push_back(*setIterator);
}
return result;
}
private:
multiset<int, greater<int> > intSet;
multiset<int, greater<int> >::iterator setIterator;
void maxHeap(vector<int> input,int k)
{
intSet.clear();
if(k < 1 || input.size() < k)
return;
for(int i = 0; i < input.size(); ++i)
{
if(static_cast<int>(intSet.size()) < k)
intSet.insert(input[i]);
else
{
setIterator = intSet.begin();
if(input[i] < *(intSet.begin()))
{
intSet.erase(setIterator);
intSet.insert(input[i]);
}
}
}
}
};