Leetcode-Array-274.H指数

https://leetcode.cn/problems/h-index/?envType=study-plan-v2&envId=top-interview-150

思路

这道题目的核心是需要计数,想到计数排序的方法,我们首先将 引用次数为 i 的数量存入 cite[i] 中,而后逆序遍历 cite 数组,就可以找到 >=i,其中

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int hIndex(int[] citations) {
// 优先考虑统计,计数排序方法
// 因为引用指数<=1000,采用计数排序 将 cite[citations[i]]++
// 从后向前,若 \sum cite[n~i]>=i 输出
// 否则继续向前累加
int[] cite = new int[1002]; // java 采用默认的初始化值,数组长度选择 min(citations[i],citations.length)+1 即可
for(int i=0;i<citations.length;i++){
cite[citations[i]]+=1;
}
int hindex=0;
for(int i=cite.length-1;i>=0;i--){
hindex+=cite[i];
if(hindex>=i)return i;
}
return 0;
}
}