题目

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

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

相关文章:

  • 2022-01-05
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-07
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
相关资源
相似解决方案