首页 > 代码库 > LeetCode-H index II
LeetCode-H index II
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。