题目链接:https://leetcode.com/problems/h-index-ii/

题目:

Follow up for H-Index: What if the citations

Hint:

  1. Expected runtime complexity is in O(log n) and the input is sorted.

思路:


用二分查找法。




算法:

 

1. public int hIndex(int[] citations) {  
2. int h = 0;  
3. int right = citations.length - 1, left = 0, mid = (right - left) / 2 + left;  
4. while (right >= left) {  
5. if (citations[mid] < citations.length - mid) {  
6. 1;  
7. else if (citations[mid] >= citations.length - mid) {  
8. 1;  
9.             h = citations.length - mid;  
10. if (mid - 1 >= 0 && citations[mid - 1] < citations.length - mid + 1)  
11. return h;  
12.         }  
13. 2 + left;  
14.     }  
15. return h;  
16. }