短路求值新用处

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 @ 2015-09-06 14:55  daijkstra  阅读(131)  评论(0编辑  收藏  举报