mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 275. H-Index II.md
This commit is contained in:
parent
b821c8f482
commit
9dab689cdd
23
solutions/275. H-Index II.md
Normal file
23
solutions/275. H-Index II.md
Normal file
@ -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<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
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in New Issue
Block a user