H-Index II

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

 1 class Solution {
 2 public:
 3     int hIndex(vector<int>& citations) {
 4         int size=citations.size();
 5         if(size==0)
 6             return 0;
 7         int res=0;
 8          
 9         int left=0;
10         int right=size-1;
11         int temp=0;
12         while(left!=right)
13         {
14              
15             int mid=(left+right)/2;
16             
17             if(citations[mid]<=size-mid)
18             {
19                 left=mid+1;
20                 temp=getMin(citations[mid],size-mid);
21                 res=getMax(temp,res);
22             }
23                 
24             else
25                 right=mid;
26             //res=getMax(res,temp);
27              
28         }
29         temp=getMin(citations[left],size-left);
30         res=getMax(temp,res);
31         return res;
32     }
33     int getMin(int a,int b)
34     {
35         return a<b?a:b;
36     }
37     int getMax(int a,int b)
38     {
39         return a>b?a:b;
40     }
41 };
原文地址:https://www.cnblogs.com/aguai1992/p/5024611.html