首页 > 其他 > 详细

LeetCode-H index II

时间:2016-08-30 13:23:30      阅读:243      评论:0      收藏:0      [点我收藏+]

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

Analysis:

Binary search: the first non-negative value for ciatations[i]-(len-i)

Solution:

 1 public class Solution {
 2     public int hIndex(int[] citations) {
 3         int len = citations.length;
 4         int start = 0, end = len-1;
 5         while (start <= end){
 6             int mid = start + (end-start)/2;
 7             if (citations[mid] >= len - mid){
 8                 end = mid-1;
 9             } else {
10                 start = mid+1;
11             }
12         }
13         return len-start;
14     }
15 }

 

LeetCode-H index II

原文:http://www.cnblogs.com/lishiblog/p/5821477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!