[LeetCode] H-Index II
来源:互联网 发布:gap淘宝旗舰店可靠吗 编辑:程序博客网 时间:2024/05/17 01:29
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.
解题思路
二分法。
实现代码
C++:
// Runtime: 12 msclass Solution {public: int hIndex(vector<int>& citations) { int len = citations.size(); int left = 0; int right = len - 1; while (left <= right) { int mid = left + (right - left) / 2; if (citations[mid] >= len - mid) { right = mid - 1; } else { left = mid + 1; } } return len -left; }};
Java:
// Runtime: 12 mspublic class Solution { public int hIndex(int[] citations) { int len = citations.length; int left = 0; int right = len - 1; while (left <= right) { int mid = left + (right - left) / 2; if (citations[mid] >= len - mid) { right = mid - 1; } else { left = mid + 1; } } return len - left; }}
0 0
- Leetcode: H-Index II
- Leetcode: H-Index II
- [LeetCode]H-Index II
- H-Index II -- leetcode
- LeetCode H-Index II
- leetcode H-Index II
- [Leetcode]H-Index II
- [LeetCode] H-Index II
- LeetCode -- H-Index II
- [LeetCode]H-Index II
- LeetCode:H-Index II
- 【Leetcode】H-Index II
- Leetcode H-Index II
- leetcode:H-Index II
- LeetCode H-index and H-index II
- leetcode之H-index &&H-index II
- [leetcode] 275.H-Index II
- leetcode 275: H-Index II
- 安卓优雅的svg实践绚丽效果
- APP中文名字
- About The Way
- JVM-深入虚拟机字节码执行引擎
- 一些非常好的开源c++代码
- [LeetCode] H-Index II
- 离群点检测方法
- 静态广播和动态广播的区别
- 为github帐号添加SSH keys
- 指针,函数,数组打杂混学习以及typedef学习笔记
- Linux 远程管理工具(SecureCRT)
- 安全参透之旅第3章 Metasploit工具第二节
- Objective-C:Day1~Day2
- 修改ubuntu中命令提示符