H index II

来源:互联网 发布:类的定义 c语言 编辑:程序博客网 时间:2024/05/18 22:41

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

二分:

public int hIndex(int[] citations) {        int len = citations.length;          int left = 0, right = len - 1;          while(left <= right){              int mid = left + (right - left) / 2;              if(citations[mid] == (len - mid)) return citations[mid];              else if (citations[mid] > (len - mid)) right = mid - 1;              else left = mid + 1;          }          return len - left;      }


0 0
原创粉丝点击