Follow up for H-Index: What if the citations
array is sorted in ascending order? Could you optimize your algorithm?
接上一篇,数组升序排列。
一看就是二分,需要注意边界,仔细想想。
class Solution {
public:
int hIndex(vector<int>& citations) {
int mid = -1, l = 0, r = citations.size()-1;
while(l <= r)
{
mid = (l + r) >> 1;
if(citations.size() - mid == citations[mid]) return citations[mid];
else if(citations.size() - mid > citations[mid]) l = mid + 1;
else r = mid - 1;
}
return citations.size() - r - 1;
}
};
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有