题目:
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.
翻译:
这个题目是模拟一个人的论文被引用的情况,假设他有5篇文章,并且每篇文章的引用次数分别为:3,0,6,1,5,那么他的h-index定义如下:所有文章数量为N,那么有h篇文章的引用数量超过或等于h,剩下N-h篇文章的引用次数都不超过h。所以在上述例子中3,6,5均大于等于3,而0,1均小于3,因此h-index为3。
思路:
将整个数组进行排序,之后从大到小进行遍历,直到找到citations不满足条件即可return,具体可以结合代码看看。
代码:
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(),citations.end());
for (int i=1;i<=citations.size();i++)
{
if(citations[citations.size()-i]<i)
return i-1;
}
return citations.size();
}
};
结果: