Update 274. H-Index.md

This commit is contained in:
ShusenTang 2019-10-31 10:12:30 +08:00 committed by GitHub
parent 7090ba5593
commit b821c8f482
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,16 +51,15 @@ private:
public: public:
int hIndex(vector<int>& citations) { int hIndex(vector<int>& citations) {
int n = citations.size(); int n = citations.size();
if(!n) return 0;
int left = 0, right = n - 1; int left = 0, right = n - 1;
while(left <= right){ while(left < right){
if(left == right) return min(citations[left], n - left);
int k = partition(citations, left, right); int k = partition(citations, left, right);
if(n - k == citations[k]) return citations[k]; if(n - k == citations[k]) return citations[k];
if(n - k > citations[k]) left = k + 1; if(n - k > citations[k]) left = k + 1;
else right = k; else right = k;
} }
return 0; return min(citations[left], n - left); // left == right
} }
}; };
``` ```