275. H-Index II

xiaoxiao2021-02-28  76

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

如果citations是升序排列,则可以使用二分查找。这道题是让找有几篇论文的引用超过了几次。判断条件为:citations[mid] < (len - mid),最后的返回值是:len - 1 - left。代码如下:

public class Solution { public int hIndex(int[] citations) { int len = citations.length; int left = -1, right = len; while (left + 1 < right) { int mid = left + (right - left) / 2; if (citations[mid] < (len - mid)) { left = mid; } else { right = mid; } } return len - 1 - left; } }

转载请注明原文地址: https://www.6miu.com/read-68476.html

最新回复(0)