LeetCode 275. H-Index II(文献)
2016-04-13 12:14
465 查看
原题网址:https://leetcode.com/problems/h-index-ii/
Follow up for H-Index: What if the
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)
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.
方法一:从大到小扫描,时间复杂度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; } }
相关文章推荐
- mysqld启动失败
- 51 nod 1182 完美字符串
- hadoop集群配置之hive1.2.0安装部署(远程mysql)
- 水果消除-----15HNUST校赛
- 非阻塞connect的实现
- 迷宫问题(栈的应用)
- CSS文字溢出部分显示为省略号
- Animation动画讲解文章
- Wireshark表达式
- 电容器在电路中的作用(很全)
- Memcached 命令行常用命令
- [Memory] A look at the x86 "Self-referencing Page Directory trick"
- request中的各种方法
- Qt相关数据类型转换
- iOS GCD最大并发控制
- maven dubbo
- SQL性能优化:如何定位网络性能问题
- javascript性能优化:创建javascript无阻塞脚本
- JAVA事务的概念
- Oracle查询列重命名