HIndex II
约 1 分钟· 本文阅读 次
本页目录
H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Hint:
Expected runtime complexity is in O(log n) and the input is sorted.
Solution
二分法(Binary Search)
public class Solution {
public int hIndex(int[] citations) {
int len = citations.length;
int low = 0;
int high = len - 1;
while (low <= high){
int mid = (low + high) / 2;
if (len - mid > citations[mid])
low = mid + 1;
else
high = mid - 1;
}
return len - low;
}
}相关文章
- 2 Sum2016-01-05
- 3 Sum Closest2016-01-05
- 3 Sum2016-01-05
- 4 Sum2016-01-05
已恢复到上次阅读位置