} /* * 275. H-Index II * 1.3 by Mingyang * 在Sort里面有线性解法的 * 输入数组是有序的,让我们在O(log n)的时间内完成计算, * 我们最先初始化left和right为0和数组长度len-1,然后取中间值mid, * 比较citations[mid]和len-mid做比较,如果前者大,则right移到mid之前, * 反之right移到mid之后,终止条件是left>right,最后返回len-left即可 * for paper[m]. there should be at least (len – m) papers with citations >= citations[m] */ public static int hIndex(int[] citations) { if (citations == null || citations.length == 0) { return 0; } int low = 0; int high = citations.length - 1; int len = citations.length; while (low <= high) { int mid = (low + high)/2; if (citations[mid] == len - mid) return len - mid; //第k个数等于k,绝配----len-mid表示这个数从大到小排在第几位 else if (citations[mid] > len - mid) high = mid - 1; else low = mid + 1; } return len - low; } //孟严的方法 public int hIndex2(int[] citations) { int len=citations.length; if(len==0) return 0; int left=0,right=len-1; while(left<=right) { int mid=left+(right-left)/2; // 0 4 5 6 7 5>=3 //4>=4 if(citations[mid]>=len-mid) //--------mid=2 { if(mid==0||citations[mid-1]