本文主要是介绍Leetcode188: 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.
class Solution {
public:int hIndex(vector<int>& citations) {int h = 0;int l = 0;int n = citations.size();int r = n-1;while(l<=r){int mid = (l+r)/2;if(n-mid <= citations[mid]){h = n-mid;r = mid-1;}elsel = mid+1;}return h;}
};
这篇关于Leetcode188: H-Index II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!