翻译
给定一个研究者的引用数(每个引用都是非负数)的数组,写一个函数用于计算研究者的h索引。
根据*对于h-index的定义:“一个科学家有索引h,如果他或他的N篇论文至少存在h个相互引用,而且其他的N-h篇论文相互引用次数不高于h。
例如,给定citations = [3, 0, 6, 1, 5],这意味着研究者总共有5篇论文,而且每一篇论文分别收到3、0、6、1、5次相互引用。
因为研究者有3篇论文存在至少3次引用(译者注:即数组的第0、2、4篇论文分别存在3、6、5次引用),而其余剩下的则不超过3次相互引用,所以他的h-index是3。
备注:如果对于h有多个可能的值,那么取最大值作为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.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
代码
这道题只要在草稿纸上,推理一下,很容易就可以写出代码,就不多说了。
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(), greater<int>());
int h = 0;
for (int i = 0; i < citations.size(); i++) {
if (i < citations[i]) {
h++;
}
}
return h;
}
};