274. H-Index

Given an array of citations (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 h citations each."less

Example:函數

Input: citations = [3,0,6,1,5]
Output: 3 
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 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.

Note: If there are several possible values for h, the maximum one is taken as the h-index.code

難度:medium ip

題目:給定一個研究人員的引用數組(每一個引用都是一個非負整數),編寫一個函數來計算研究人員的h-index。
根據維基百科關於h-index的定義,一個科學家的h-index爲h,即其N篇文章中有h篇的引用不低於h, 而且其它文章引用數不超過h.ci

思路:弄清題義便可。rem

Runtime: 3 ms, faster than 100.00% of Java online submissions for H-Index.
Memory Usage: 37.2 MB, less than 65.22% of Java online submissions for H-Index.it

class Solution {
    public int hIndex(int[] citations) {
        if (null == citations || citations.length < 1) {
            return 0;
        }
        Arrays.sort(citations);
        int n = citations.length;
        int hIndex = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (citations[i] >= n - i) {
                hIndex++;
            } else {
                break;
            }
        }
        
        return hIndex;
    }
}
相關文章
相關標籤/搜索