H-Index II
来源:互联网 发布:java主流工作流引擎 编辑:程序博客网 时间:2024/05/19 13:24
题目名称
H-Index II—这里写链接内容
描述
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
分析
这里给的是一个已经排序的数组,如果用之前在H-Index中的算法效率非常低,所以我想到用二分搜索来查找。
C++代码
class Solution {public: int hIndex(vector<int>& citations) { if(citations.size()==0) return 0; int left=0, len = citations.size(), right= len-1, mid; while(left<=right) { mid=(left+right)>>1; if(citations[mid]== (len-mid)) return citations[mid]; else if(citations[mid] > (len-mid)) right = mid - 1; else left = mid + 1; } return len - (right+1); }};
总结
弄懂二分搜索的含义,代码就很好理解,确定一个搜索范围,判断这个范围中间的数是否是符合要求的,如果不是,根据中间位置将搜索区间分为两半,继续查找可能的搜索域,直到找到我们要的数。
0 0
- H-Index && H-Index II
- Leetcode: H-Index II
- Leetcode: H-Index II
- H index II
- [LeetCode]H-Index II
- H-Index II -- leetcode
- LeetCode H-Index II
- leetcode H-Index II
- [275]H-Index II
- [Leetcode]H-Index II
- [LeetCode] H-Index II
- H-Index II
- LeetCode -- H-Index II
- 275、H-Index II
- Leetcode188: H-Index II
- H-Index II
- H-Index II
- 275. H-Index II
- mysql执行计划
- android orm工具GreenDao的使用
- HDU 1238 Substrings 细心的模拟
- Heritrix3.3.0源码阅读 crawler-beans.cxml中URI过滤规则的配置
- going deeper with convolutions笔记
- H-Index II
- 编程之美-寻找发帖“水王”方法整理
- BZOJ 1486: [HNOI2009]最小圈
- 第一写博客
- Search for a Range
- [转]STM32学习笔记6-定时器中断
- 3D Touch
- CString、TCHAR*、char*转换
- beginEdit()和endEdit()到底用来干吗的?