LeetCode:H-Index II
2016-06-01 19:20
197 查看
H-Index II
Total Accepted: 24331 TotalSubmissions: 74459 Difficulty: Medium
Follow up for H-Index:
What if the
citationsarray is sorted in ascending order? Could you optimize your algorithm?
Hint:
Expected runtime complexity is in O(log n) and the input is sorted.
Subscribe to see which companies asked this question
Hide Tags
Binary Search
Hide Similar Problems
(M) H-Index
思路:
承接上题【】,与上题不同之处在于已经是排序的,因此通过二分找到临界点即可。
java code:
public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) return 0; int len = citations.length; int lo = 0, hi = len-1; while(lo <= hi) { int mid = lo + (hi-lo)/2; if(citations[mid] == len - mid) return len-mid; else if(citations[mid] >= len - mid) hi = mid - 1; else lo = mid + 1; } return len-lo; } }
相关文章推荐
- 第十三周项目—阅读、修改和运行关于交通工具类的程序(1)
- Hibernate+Spring整合时报错
- 第二次冲刺周期站立会议(6)
- (Caffe)基本类Solver、Caffe、Batch(二)
- Verilog 参数化和状态机
- (Caffe)基本类Blob,Layer,Net(一)
- 【NOIP2013模拟】归途与征程 题解+代码
- 数据库表空间相关
- hbase1.1.3 启动一
- 安卓获取屏幕分辨率
- jQuery查找——parent/parents/parentsUntil/closest
- [置顶] Android引导蒙层,安卓新手引导图,引导图层,支持椭圆,圆形,矩形多种形状,一行代码快速搞定
- c++11之智能指针
- Hadoop总结篇之三---一个Job到底被提交到哪去了
- 6-7 8255 控制交通灯
- Linux Shell系列教程之(七)Shell输出
- Android开发中解析、创建Bitmap对象时OOM的有效解决方法并附上一些干货
- 说说mvc模式的原理,它在android中的运用
- 第十一周项目训练8.3 圆,圆柱类族的设计
- HDU 2516取石子游戏(巴什博弈)