Follow up for H-Index: What if the citations
array is sorted in ascending order? Could you optimize your algorithm?
因为排好序了,和上一篇的方法一致,不过时间上差太多。。。
用二分法进行优化
public class Solution {
public int hIndex(int[] citations) {
int len=citations.length, left=0, right=len-1, mid=0;
while(left<=right)
{
mid=left+(right-left)/2;
if(citations[mid] >= (len-mid)) right = mid - 1;
else left = mid + 1;
}
return len - left;
}
}