码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]H-Index II

时间:2015-11-29 08:11:47      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

二分搜索

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

 

[LeetCode]H-Index II

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5003984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!