mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
930 B
930 B
275. H-Index II
思路
这题实际上是274. H-Index的简化版,274题引用列表没有排好序此题已经排序了。 所以会做274题的话此题肯定没问题,274题思路二其实就和此题的解法类似,复杂度为O(logn),这里不再赘述。
C++
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
if(!n) return 0;
int left = 0, right = n - 1;
while(left < right){
int mid = (left + right) >> 1;
if(n - mid == citations[mid]) return citations[mid];
if(n - mid > citations[mid]) left = mid + 1;
else right = mid;
}
return min(citations[left], n - left); // left == right
}
};