LeetCode 275. H-Index II

2020-05-12 17:14:54 浏览数 (1)

题目

现在变了,数列是拍好序的,题目要求对数效率,因为x只可能有一个那就二分咯

代码语言:javascript复制
class Solution {
public:
    int hIndex(vector<int>& citations) {
        
        if(citations.size()==0)
            return 0;
      
        int len = citations.size();
        int l=0;
        int r = len-1;
        
        while(l<=r)
        {
            int mid = (l r)/2;
            
            if(citations[mid]>len-mid)
            {
                r = mid-1;
            }
            else if(citations[mid]<len-mid)
            {
                l = mid 1;
            }
            else
            {
                return len-mid;
            }
        }
        
        if(l<len&&citations[l]>len-l)
            return len-l;
        else
            return 0;
        
    }
};

0 人点赞