《leetCode》:HIndex II
来源:互联网 发布:手机淘宝退货退款流程 编辑:程序博客网 时间:2024/06/03 07:14
题目
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Hint:
Expected runtime complexity is in O(log n) and the input is sorted.
思路
由于数组是已经排序的,利用二分查找的思想来做即可
实现代码如下:
//在N篇paper中至少有h篇论文大于等于h,则才返回h。N篇paper最大值返回N public int hIndex(int[] citations) { if(citations==null||citations.length==0){ return 0; } int len = citations.length; //特殊情况,citations中的最小值都大于len if(citations[0]>=len){ return len; } //由于citations是一个升序数组 int begin = 0; int end = len-1; while(begin<=end){ int mid =(begin+end)/2; if(citations[mid]==len-mid){//存在len-mid篇paper影响因子大于等于citations[mid] return citations[mid]; } else if(citations[mid]<len-mid){//次数过多,看是否还存在更大的 begin = mid+1; } else if(citations[mid]>len-mid){//次数过少, end = mid -1; } } //如果在上面的while循坏中都没有找到citations[mid]==len-mid, //则可以说明一点的是在数组中存在len-(end+1)个影响因子大于len-(end+1) return len-(end+1);
1 0
- 《leetCode》:HIndex II
- leetcode hIndex implemented with python
- 华为hindex split
- Hindex--华为Hbase二级索引
- LeetCode: Combination Sum II
- LeetCode: Combination Sum II
- LeetCode: Jump Game II
- LeetCode: Unique Paths II
- LeetCode: Subsets II
- LeetCode: Permutations II
- LeetCode: Spiral Matrix II
- LeetCode: N-Queens II
- LeetCode: Path Sum II
- [LeetCode]Combination Sum II
- LeetCode Combination Sum II
- LeetCode Jump Game II
- LeetCode Spiral Matrix II
- LeetCode SubSets II
- BZOJ 2763 飞行路线(最短路)
- ToggleButton
- AbstractQueuedSynchronizer(十四)——应用之FutureTask
- csapp第二章学习笔记
- 【NOIP模拟】项链
- 《leetCode》:HIndex II
- OBIEE提示-不同维度关联
- Spring-8:SpEL入门
- 35、面向对象
- Rxjava基本用法
- hibernate缓存机制详细分析
- UEStudio中进行文件编码转换
- 求素数为什么到平方根就行了
- Big data and its developer fallout