题解 | #查找第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;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
今天 14:35
点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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