275. H指数 II
给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列。编写一个方法,计算出研究者的 h 指数。
h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)至多有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数不多于 h 次。)"
解题思路: 按照H指数定义遍历一遍所有h值,找出最大的h值即可。此算法时间复杂度为
O
(
N
)
O(N)
O(N).
class Solution {
public:
int hIndex(vector<int>& citations) {
if (citations.empty()) return 0;
int n = citations.size(), res = 0;
for (int i = n - 1; i >= 0; --i) {
int h = n - i;
if (citations[i] >= h) {
if (i == 0) res = max(res, h);
else if (citations[i - 1] <= h) res = max(res, h);
}
}
return res;
}
};