【二分查找】275. H指数 II

题目:

 

 

 

解答:

 

 

 1 class Solution {
 2 public:
 3     int hIndex(vector<int>& citations) 
 4     {
 5         int n = citations.size();
 6 
 7         int pivot;
 8         int left = 0;
 9         int right = citations.size() - 1;
10 
11         while (left <= right) 
12         {
13             pivot = left + (right - left) / 2;
14             if (citations[pivot] == n - pivot) 
15             {
16                 return n - pivot;
17             }
18             else if (citations[pivot] < n - pivot) 
19             {
20                 left = pivot + 1;
21             }
22             else 
23             {
24                 right = pivot - 1;
25             }
26         }
27         return n - left;
28     }
29 };

 

posted @ 2020-05-05 13:13  梦醒潇湘  阅读(159)  评论(0)    收藏  举报