-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGetLeastNumbers_Solution.h
134 lines (123 loc) · 3.68 KB
/
GetLeastNumbers_Solution.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// GetLeastNumbers_Solution.h
// other
//
// Created by junlongj on 2019/8/4.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef GetLeastNumbers_Solution_hpp
#define GetLeastNumbers_Solution_hpp
#include <stdio.h>
#include <vector>
#include <set>
/*
剑指Offer(二十九):最小的K个数
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf?tpId=13&tqId=11182&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
namespace codinginterviews {
#pragma mark - v1
std::vector<int> GetLeastNumbers_Solution_STL(std::vector<int> input, int k){
typedef std::multiset<int, greater<int>> set;
typedef std::multiset<int, greater<int>>::iterator iterator;
set s;
int i;
for(i = 0; i < input.size(); i++){
if (s.size() < k){
s.insert(input[i]);
}else{
if (input[i] < *s.begin()){
s.erase(s.begin());
s.insert(input[i]);
}
}
}
iterator begin = s.begin();
vector<int> result;
while(begin != s.end()){
result.push_back(*begin++);
}
return result;
}
#pragma mark - v2
void swap_(int &a,int &b){
int temp = a;
a = b;
b = temp;
}
void heapify(std::vector<int> &input,int k, int index){
//判断叶子节点是不是比跟小.
while (true) {
int lc = 2*index+1;
if (lc+1 <k &&input[lc] < input[lc+1]) {
lc++;
}
if (lc < k && input[lc] > input[index]) {
swap_(input[lc], input[index]);
index = lc;
}else{
break;
}
}
}
std::vector<int> GetLeastNumbers_Solution(std::vector<int> input, int k) {
if (input.size() < k ) {
return input;
}
std::vector<int> heap(input.begin(),input.begin()+k);
//创建大顶堆
for (int i=k/2-1; i>=0; i--) {
//跳转堆结构
heapify(heap, k, i);
}
for (int i=k; i<input.size(); i++) {
if (input[i] < heap[0]) {
heap[0] = input[i];
heapify(heap, k, 0);
}
}
//排序
for (int i=k-1; i>0; i--) {
swap_(heap[0], heap[i]);
heapify(heap, --k, 0);
}
return heap;
}
#pragma mark - v2
int partition(vector<int> &input,int start, int end){
int temp = input[end];
int i,j;
i=j=start;
for(;j<=end;j++){
if(input[j] < temp){
swap(input[j],input[i++]);
}
}
swap(input[i], input[end]);
return i;
}
vector<int> GetLeastNumbers_Solution2(vector<int> input, int k) {
if (input.empty() || k>input.size()||k<=0)
return {};
int start = 0;
int end = input.size()-1;
int index = partition(input , start , end);
while (index != k-1){
if (index < k-1){
start = index + 1;
}else{
end = index - 1;
}
index = partition(input , start , end);
}
vector<int> results(input.begin(), input.begin()+k);
return results;
}
void test_GetLeastNumbers_Solution(){
std::vector<int> a{0,0,1,2,4,2,2,3,1,4};
std::cout << "test_GetLeastNumbers_Solution starting,....." << std::endl;
std::vector<int> b = GetLeastNumbers_Solution(a, 8);
b;
}
}
#endif /* GetLeastNumbers_Solution_hpp */