面试经典150题 day11
题目来源
力扣每日一题;题序:274
我的题解
方法一 排序+从后往前遍历
public int hIndex(int[] citations) {
int n=citations.length;
Arrays.sort(citations);
int count=0;//表示h值时,被引用次数大于等于 h的论文数
int index=n;//表示h值
for(int i=n-1;i>=0;i--){
//当引用值大于h时,需要将count加1
if(citations[i]>=index)
count++;
else{
//若被引用次数大于等于 h的论文数大于h则直接返回h值,因为是从大往小遍历的
if(count>=index)
return index;
//更新h值
while(citations[i]<index&&count<index)
index--;
//当引用值大于h时,需要将count加1
if(citations[i]>=index)
count++;
}
}
//返回最终的h值
return index;
}
方法二 计数排序+后缀和
public int hIndex(int[] citations) {
int n=citations.length;
int[] count=new int[n+1];
for(int i=0;i<n;i++){
if(citations[i]>=n)
count[n]++;
else
count[citations[i]]++;
}
for(int i=n;i>=0;i--){
if(i!=n)
count[i]+=count[i+1];
if(count[i]>=i)
return i;
}
return 0;
}
方法三 排序+从左到右遍历
public int hIndex(int[] citations) {
Arrays.sort(citations);
int h=0;
int right=citations.length-1;
while(right>=0&&citations[right]>h){
h++;
right--;
}
return h;
}
有任何问题,欢迎评论区交流,欢迎评论区提供其它解题思路(代码),也可以点个赞支持一下作者哈😄~