[LeetCode]H-Index II

二分搜索

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

 

posted @ 2015-11-29 06:56  Weizheng_Love_Coding  阅读(120)  评论(0编辑  收藏  举报