当前位置:网站首页>53. 最小的k个数
53. 最小的k个数
2022-08-02 02:20:00 【Hunter_Kevin】
53. 最小的k个数
题目
输入 n 个整数,找出其中最小的 k 个数。
注意:
输出数组内元素请按从小到大顺序排序;
数据范围
1≤k≤n≤1000
样例
输入:[1,2,3,4,5,6,7,8] , k=4
输出:[1,2,3,4]
思路一:小根堆,输出前k个堆顶元素
0
1 2
3 4 5 6
7 8
当堆数组长度n为9时,则从下标为3开始往上down操作,即从9/2 - 1 == 3
down()中对应的左孩子节点下标为2*u+1, 右孩子节点下标为2*u+2
class Solution {
public:
void down(vector<int>& input, int u, int len){
int t = u;
//此处是数组的下标,限制不越界即可
if(2*u+1 <= len && input[2*u+1] < input[t]) t = 2*u+1;
if(2*u+2 <= len && input[2*u+2] < input[t])t = 2*u+2;
if(t != u){
swap(input[u],input[t]);
down(input,t,len);//递归对交换的位置的元素执行down()
}
}
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
//进行堆排序
int len = input.size()-1;
//数组下标从0开始,如果数组长度为n,则从数组下标为n/2 -1的地方开始往上执行down()
for(int i = input.size()/2 - 1; i>=0; i--) down(input, i, len);
vector<int> res;
while(k--){
res.push_back(input[0]);//添加到vector中
input[0] = input[len];//把堆中的末尾元素放到堆顶
len--;//存储堆的数组长度减少
down(input,0,len);//对堆顶执行down
}
return res;
}
};
思路二:维护一个k个数的大根堆
C++中的大根堆是优先队列
priority_queue<>
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
//遍历数组,维护一个堆个数为k的大根堆,最后将大根堆放到vector中,此时序列从大到小,再反转数组即可
priority_queue<int> heap;
// 遍历给定的数组
for(auto num : input){
heap.push(num);
if(heap.size() > k) heap.pop();
}
vector<int> res;
// 把维护的大根堆中的k个数放到数组中
while(heap.size()){
res.push_back(heap.top());
heap.pop();
}
// 反转数组
reverse(res.begin(),res.end());
return res;
}
};
边栏推荐
- swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
- LeetCode brushing diary: 53, the largest sub-array and
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
- leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
- 60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
- The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
- 字符串常用方法
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
- ¶ Backtop back to the top is not effective
猜你喜欢

记一个gorm初始化的坑

GTK RGB图像绘制

Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案

From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.

Power button 1374. Generate each character string is an odd number

Talking about the "horizontal, vertical and vertical" development trend of domestic ERP

Scheduled tasks for distributed applications in Golang

openGauss切换后state状态显示不对

Use baidu EasyDL implement factory workers smoking behavior recognition

Outsourcing worked for three years, it was abolished...
随机推荐
Handwritten Blog Platform ~ Day Two
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
通用客户端架构
字典常用方法
Redis Subscription and Redis Stream
AI目标分割能力,无需绿幕即可实现快速视频抠图
Oracle19c安装图文教程
Redis Persistence - RDB and AOF
messy website
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
nacos startup error, the database has been configured, stand-alone startup
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
2022-07-30 mysql8执行慢SQL-Q17分析
NIO‘s Sword(牛客多校赛)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
The state status is displayed incorrectly after the openGauss switch
2022-08-01 Install mysql monitoring tool phhMyAdmin
【web】Understanding Cookie and Session Mechanism
Chopper webshell feature analysis