classSolution { publicinthIndex(int[] citations) { Arrays.sort(citations); inth=0, i = citations.length - 1; while (i >= 0 && citations[i] > h) { h++; i--; } return h; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11
publicclassSolution { publicintHIndex(int[] citations) { Array.Sort(citations); int h = 0, i = citations.Length - 1; while (i >= 0 && citations[i] > h) { h++; i--; } return h; } }
[sol1-Python3]
1 2 3 4 5 6 7 8
classSolution: defhIndex(self, citations: List[int]) -> int: sorted_citation = sorted(citations, reverse = True) h = 0; i = 0; n = len(citations) while i < n and sorted_citation[i] > h: h += 1 i += 1 return h
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9
var hIndex = function(citations) { citations.sort((a, b) => a - b); let h = 0, i = citations.length - 1; while (i >= 0 && citations[i] > h) { h++; i--; } return h; };
[sol1-Golang]
1 2 3 4 5 6 7
funchIndex(citations []int) (h int) { sort.Ints(citations) for i := len(citations) - 1; i >= 0 && citations[i] > h; i-- { h++ } return }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: inthIndex(vector<int>& citations){ sort(citations.begin(), citations.end()); int h = 0, i = citations.size() - 1; while (i >= 0 && citations[i] > h) { h++; i--; } return h; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
intcmp(int *a, int *b) { return *a - *b; }
inthIndex(int *citations, int citationsSize) { qsort(citations, citationsSize, sizeof(int), cmp); int h = 0, i = citationsSize - 1; while (i >= 0 && citations[i] > h) { h++; i--; } return h; }
publicclassSolution { publicintHIndex(int[] citations) { int n = citations.Length, tot = 0; int[] counter = newint[n + 1]; for (int i = 0; i < n; i++) { if (citations[i] >= n) { counter[n]++; } else { counter[citations[i]]++; } } for (int i = n; i >= 0; i--) { tot += counter[i]; if (tot >= i) { return i; } } return0; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defhIndex(self, citations: List[int]) -> int: n = len(citations); tot = 0 counter = [0] * (n+1) for c in citations: if c >= n: counter[n] += 1 else: counter[c] += 1 for i inrange(n, -1, -1): tot += counter[i] if tot >= i: return i return0
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var hIndex = function(citations) { let n = citations.length, tot = 0; const counter = newArray(n + 1).fill(0); for (let i = 0; i < n; i++) { if (citations[i] >= n) { counter[n]++; } else { counter[citations[i]]++; } } for (let i = n; i >= 0; i--) { tot += counter[i]; if (tot >= i) { return i; } } return0; };
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funchIndex(citations []int) (h int) { n := len(citations) counter := make([]int, n+1) for _, citation := range citations { if citation >= n { counter[n]++ } else { counter[citation]++ } } for i, tot := n, 0; i >= 0; i-- { tot += counter[i] if tot >= i { return i } } return0 }
classSolution { public: inthIndex(vector<int>& citations){ int n = citations.size(), tot = 0; vector<int> counter(n + 1); for (int i = 0; i < n; i++) { if (citations[i] >= n) { counter[n]++; } else { counter[citations[i]]++; } } for (int i = n; i >= 0; i--) { tot += counter[i]; if (tot >= i) { return i; } } return0; } };
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
inthIndex(int *citations, int citationsSize) { int n = citationsSize, tot = 0; int counter[n + 1]; memset(counter, 0, sizeof(counter)); for (int i = 0; i < n; i++) { if (citations[i] >= n) { counter[n]++; } else { counter[citations[i]]++; } } for (int i = n; i >= 0; i--) { tot += counter[i]; if (tot >= i) { return i; } } return0; }
我们需要找到一个值 h,它是满足「有 h 篇论文的引用次数至少为 h」的最大值。小于等于 h 的所有值 x 都满足这个性质,而大于 h 的值都不满足这个性质。同时因为我们可以用较短时间(扫描一遍数组的时间复杂度为 $O(n)$,其中 $n$ 为数组 $\textit{citations}$ 的长度)来判断 x 是否满足这个性质,所以这个问题可以用二分搜索来解决。
/** * @param {number[]} citations * @return {number} */ var hIndex = function(citations) { let left = 0, right = citations.length while (left<right){ // +1 防止死循环 let mid = Math.floor((left + right + 1) / 2) let cnt = 0 for (let v of citations){ if (v >= mid){ cnt+=1 } } if(cnt>=mid){ // 要找的答案在 [mid,right] 区间内 left=mid }else{ // 要找的答案在 [0,mid) 区间内 right=mid-1 } } return left };