题解 | #寻找第K大#

寻找第K大

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

#include "map"

class Solution {
  public:
    int findKth(vector<int> a, int n, int K) {
        map<int, int> pool;
        for (int i = 0; i < n; i++) {
            auto tmp = pool.find(a[i]);
            if (tmp == pool.end()) { //没有就添加
                pool.insert({a[i],1});
            } else { //有就添加
                tmp->second += 1;
            }
        }
        auto inter = pool.begin();
        int tem = 0;
        for (; inter != pool.end(); inter++) {
            tem += inter->second;
            if(tem>=(n-K+1)){
                return inter->first;
            }
        }
        return 0;
    }
};

#算法练习#
全部评论

相关推荐

就用这个吧:支持多益再加一个空气使用费
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务