[Leetcode学习-java]H-Index II(求H引用)

问题:

难度:easy

说明:

题目主要是审题:

Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than citations each."

如果前N篇文章至少有H个引用,那么N-h个文章不会超过H个引用,其实意味着,arr[i] == N-h就是答案了,如果没有就返回比这个大的。

问题链接:https://leetcode.com/problems/validate-ip-address/

输入案例:

Example:

Input: citations = [0,1,3,5,6]
Output: 3 
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had 
             received 0, 1, 3, 5, 6 citations respectively. 
             Since the researcher has 3 papers with at least 3 citations each and the remaining 
             two with no more than 3 citations each, her h-index is 3.

我的代码:

主要是审题

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

猜你喜欢

转载自blog.csdn.net/qq_28033719/article/details/106849584