leetcode [274] - H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input:citations = [3,0,6,1,5]Output: 3 Explanation:[3,0,6,1,5]means the researcher has5papers in total and each of them had received3, 0, 6, 1, 5citations respectively. Since the researcher has3papers with at least3citations each and the remaining two with no more than3citations each, her h-index is3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
题目大意:
给定一位研究者论文被引用次数的数组,输出研究者的 h 指数。h指数指他的 (N 篇论文中)至多有 h 篇论文分别被引用了至少 h 次。
理 解:
首先对数组从大到小排序,使用二分法进行比较。
若 当前引用篇数 <= 当前最小引用数量,则向右继续找。保存当前指数=当前引用篇数,更新最大h指数。
若 当前引用篇数 > 当前最小引用数量,则向左找。
代 码 C++:
class Solution { public: int hIndex(vector<int>& citations) { int n = citations.size(); if(n<=0) return 0; if(n==1 && citations[0]>=1) { return 1; } int i,j,temp; for(i=0;i<n;++i){ for(j=i;j<n;++j){ if(citations[i]<citations[j]){ temp = citations[i]; citations[i] = citations[j]; citations[j] = temp; } } } if(n<=citations[n-1])return n; int left=0,right=n-1,mid; int h = 0; while(left<right){ mid = (left+right)/2; if(mid+1<=citations[mid]){ if(mid+1>h) h = mid+1; left = mid+1; }else{ right = mid; } } return h; } };

浙公网安备 33010602011771号