天天看點

LeetCode 274 H-Index (H索引)

版權聲明:轉載請聯系本人,感謝配合!本站位址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51753099

翻譯

給定一個研究者的引用數(每個引用都是非負數)的數組,寫一個函數用于計算研究者的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;
}
};           

繼續閱讀