首页 > 代码库 > LeetCode-H index II

LeetCode-H index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Analysis:

Binary search: the first non-negative value for ciatations[i]-(len-i)

Solution:

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

 

LeetCode-H index II