本文共 1116 字,大约阅读时间需要 3 分钟。
代码写的很烂。按下不表
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;}
转载地址:http://icuvb.baihongyu.com/