代码写的很烂。按下不表
public class Solution {
public int hIndex(int[] citations) {
if(citations.length==0)
{
return 0;
}
boolean allzearo=true;
for (int i : citations) {
if(i!=0)
{
allzearo=false;
}
}
if(allzearo)
{
return 0;
}
int left=0;
int n=citations.length;
int right=n-1;
int middle;
int index=0;
while(left<=right)
{
middle=(left+right)/2;
if(citations[n-1-middle]>middle)
{
left=middle+1;
index=middle;
}
else
{
right=middle-1;
}
}
return index+1;
}
}
牛人的代码
int hIndex(int* citations, int citationsSize) {
int lo = 0, hi = citationsSize, mid, index = 0;
while (lo <= hi) {
mid = lo + ((hi - lo) >> 1);
if (citations[citationsSize - mid - 1] > mid) {
lo = mid + 1;
index = lo;
} else {
hi = mid - 1;
}
}
return index;
}
本文探讨并对比了两种学术论文引用计数算法的实现方式:一种是使用传统双指针方法的普通算法,另一种是借鉴二分查找思想进行优化的高效算法。通过实验证明,优化后的算法在处理大规模数据集时显著提高了效率,适用于各种引用计数场景。
368

被折叠的 条评论
为什么被折叠?



