您的位置:首页 > 其它

leetcode 275: H-Index II

2015-09-05 17:09 369 查看
Follow up for
H-Index: What if the
citations
array is sorted in ascending order? Could you optimize your algorithm?

[思路]
二分查找,

[CODE]

public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;

int low=0, high=n-1;

while(low<=high) {
int mid  = low + (high-low)/2;
if(citations[mid] == n-mid) return n-mid;
else if(citations[mid] < n-mid) low = mid+1;
else high = mid-1;
}
return n-low;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: