码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-H index II

时间:2016-08-30 13:25:34      阅读:141      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!