leetcode 275: H-Index II

来源:互联网 发布:网络型数据采集机 编辑:程序博客网 时间:2024/05/21 12:46

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;    }}


0 0