题解 | #寻找第K大#

寻找第K大

http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf

class Solution {
public:
    int findKth(vector<int> a, int n, int K) {
        // write code here
        //1.sort
        /*sort(a.begin(),a.end());
        return a[n-K];*/


        //2.大顶堆
        /*priority_queue<int,vector<int>,greater<int>> small_queue;

        for(int i=0;i<K;i++){
            small_queue.push(a[i]);
        }

        for(int i=K;i<n;i++){
            if(a[i]>small_queue.top()){
                small_queue.pop();   //一定要维护k大小,切记要弹出元素
                small_queue.push(a[i]);
            }
        }

        return small_queue.top();*/

        //2.冒泡的思路
        for(int i=0;i<K;i++){
            for(int j=n-1;j>i;j--){
                if(a[j]>a[j-1])
                    swap(a[j],a[j-1]);
                }
            }
        return a[K-1];         

        //3.快排的思路




    }
};
全部评论

相关推荐

06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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