您的位置:首页 > 编程语言 > C语言/C++

leetcode_c++:哈希:H-Index(274)

2016-07-10 14:41 519 查看
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.

O(nlgn)

对于给定的而一个数组,可以先进行排序。逐渐找出引用最多的文章加入到集合中,每次加入一个元素集合就增大一个,也就是h增加一。当加入一个新的元素是,这个元素的引用次数小于h+1时就返回此时的h,否则,将h加1。

以上。

class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int h = 0;
auto iter = citations.rbegin();
while(iter != citations.rend()) {
h++;
if(*iter < h)return h - 1;
iter++;
}
return h;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: