码迷,mamicode.com
首页 > 其他好文 > 详细

短路求值新用处

时间:2015-09-06 16:00:19      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
};

 

短路求值新用处

标签:

原文地址:http://www.cnblogs.com/daijkstra/p/4785604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!