题解 | #查找第K小数#

查找第K小数

https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd

#include <iostream>
#include <queue>

using namespace std;
int main(){
    int n,k;
    while (cin>>n){
        priority_queue<int,vector<int>,greater<int>> myQueue;
        int num;
        for (int i = 0; i < n; ++i) {
            cin>>num;
            myQueue.push(num);
        }
        cin>>k;
        while (--k){//由于要寻找第k小的数,所以将前面k-1小的数都弹出
            num = myQueue.top();
            while (myQueue.top()==num)
                myQueue.pop();
        }
        cout<<myQueue.top()<<endl;
    }
}

全部评论

相关推荐

CARLJOSEPH...:宝宝你戾气太大了
点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:27
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务