LeetCode 275. H-Index II(文献)
来源:互联网 发布:布袋寅泰 知乎 编辑:程序博客网 时间:2024/05/21 11:07
原题网址:https://leetcode.com/problems/h-index-ii/
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.
方法一:从大到小扫描,时间复杂度O(n)
public class Solution { public int hIndex(int[] citations) { int h=0; for(int i=citations.length-1; i>=0 && citations[i]>h; i--,h++); return h; }}方法二:二分法查找,时间复杂度O(logn)
public class Solution { public int hIndex(int[] citations) { int i=0, j=citations.length-1; while (i<=j) { int m = (i+j) >> 1; if (citations[m] == citations.length-m) return citations.length-m; if (citations[m] > citations.length-m) j=m-1; else i=m+1; } return citations.length-i; }}
另一种实现:
public class Solution { public int hIndex(int[] citations) { int i=0, j=citations.length-1; while (i<=j) { int m=(i+j)/2; if (citations[m]>=citations.length-m) j=m-1; else i=m+1; } return citations.length-i; }}
0 0
- LeetCode 275. H-Index II(文献)
- LeetCode 274. H-Index(文献)
- [leetcode] 275.H-Index II
- 275. H-Index II LeetCode
- leetcode 275. H-Index II
- LeetCode *** 275. H-Index II
- leetcode.275. H-Index II
- LeetCode - 275. H-Index II
- [leetcode] 275. H-Index II
- LeetCode 275. H-Index II
- 【LeetCode】 275. H-Index II
- Leetcode 275. H-Index II
- [LeetCode]275. H-Index II
- leetcode 275. H-Index II
- leetcode 275. H-Index II
- Leetcode: H-Index II
- Leetcode: H-Index II
- [LeetCode]H-Index II
- hadoop集群配置之hive1.2.0安装部署(远程mysql)
- 20160413ceph simple install
- 迷宫问题(栈的应用)
- 做为一个前端工程师,是往node方面转,还是往HTML5方面转
- 山东省第二届ACM大学生程序设计竞赛:The Android University ACM Team Selection Contest
- LeetCode 275. H-Index II(文献)
- 取消标识列设定
- 蘑菇街2016校招算法题
- 基于Firebreath的浏览器插件制作
- php 5.3新增的闭包语法介绍function() use() {}
- Git SSH Key 生成步骤
- 静态检测内存泄露Analyze--Value stored to 'dataArr' during its initialization is never read
- LeetCode 39. Combination Sum
- Android Framework的启动过程