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.
中文:
H-Index是衡量一个人学术水平的指数。接下来我们简称h。
h代表“高引用次数”(high citations),一名科研人员的h指数是指他至多有h篇论文分别被引用了至少h次。h指数能够比较准确地反映一个人的学术成就。一个人的h指数越高,则表明他的论文影响力越大。例如,某人的h指数是20,这表示他已发表的论文中,每篇被引用了至少20次的论文总共有20篇。------By 百度百科。
传入一个citations的Vector,每一个元素代表每一篇论文的被引用次数。求出这个citations的H-index。比如[3, 0, 6, 1, 5]中对于下标0的论文,值为3,那么要求至少有3篇论文的引用数要大于等于3。检查有下标为3,5,6;共计三篇论文的引用次数超过了。H-Index = 3。如是依次检查剩下的元素。
我的思路:
先对Vector从低到高排序。那么根据H-index的定义我们只要找到这个一个元素i。算上i,到Vector末尾,这区间中的元素数量要大于等于i的值。
编辑器不知道怎么始终贴不上代码片段。只好直接粘贴了。
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(),std::less<int>());
int length = citations.size();
int hIndex = 0;
for (int i = 0; i < length; i++)
{
int temp = 0;
//他已发表的论文中,每篇被引用了至少20次的论文总共有20篇->某人的h指数是20
if (length - i <= citations[i])
{
hIndex = length-i;
break;
}
}
return hIndex;
}
};