Leetcode解题报告——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."
For example, given citations = [3, 0, 6, 1, 5]
, which 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, his h-index is 3
.
题目大意:
给定一个数组代表学者的每篇文章被引用的次数,找出最大的 H-Index。
h-index 定义为:学者有 n 篇文章,其中有 h 篇 的引用次数大于 h
解题思路:
先将数组排序,然后依次遍历:
对于文章 i ,记引用次数大于等于该文章的文章数为 h ,即 h = length - i,如果
citations[i] >= h ,则 h 为 一个H-Index
代码示例:
public int hIndex(int[] citations) {
if(citations.length < 1) return 0;
Arrays.sort(citations);
int result = 0;
for (int i = 0; i < citations.length; i++) {
if(citations[i] >= citations.length -i )
result = Math.max(result,citations.length -i);
}
return result;
}