diff --git a/solutions/275. H-Index II.md b/solutions/275. H-Index II.md new file mode 100644 index 0000000..428a8b5 --- /dev/null +++ b/solutions/275. H-Index II.md @@ -0,0 +1,23 @@ +# [275. H-Index II](https://leetcode.com/problems/h-index-ii/) +# 思路 +这题实际上是[274. H-Index](https://leetcode.com/problems/h-index/)的简化版,274题引用列表没有排好序此题已经排序了。 +所以会做274题的话此题肯定没问题,[274题思路二](https://github.com/ShusenTang/LeetCode/blob/master/solutions/274.%20H-Index.md)其实就和此题的解法类似,复杂度为O(logn),这里不再赘述。 + +# C++ +``` C++ +class Solution { +public: + int hIndex(vector& 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 + } +}; +```