短路求值新用处

https://leetcode.com/problems/h-index/

复制代码
class Solution {
public:
    int hIndex(vector<int>& citations) {
        priority_queue<int> pq;
        for(int i=0;i<citations.size();i++){
            pq.push(citations[i]);
        }
        int pages=0;
        while(!pq.empty()){
            int cur = pq.top();
            pq.pop();
            pages++;
            // 在取下一个值时候先判定是否为空
            if(cur >= pages && (pq.empty() || pages >= pq.top())) return pages;
        }
        
        return 0;
    }
};
复制代码

 

posted @   daijkstra  阅读(133)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示