LeetCode 经典 150 · 可视化
逐步动画、变量流转与五语言代码参考
#274 · 数组 / 字符串
H 指数
H-Index
citations = [3,0,6,1,5]线性序列4 个关键状态
步骤 1建立 0..n 的引用次数桶。
count=[1,1,0,1,0,2]1
01
10
21
30
42
5推荐
计数桶
时间
O(n)空间 O(n)超过 n 的引用数都归入 n 桶,再从高到低累计论文数。
1function hIndex(citations) {2 const n = citations.length,3 count = Array(n + 1).fill(0);4 for (const c of citations) count[Math.min(c, n)]++;5 let papers = 0;6 for (let h = n; h >= 0; h--) {7 papers += count[h];8 if (papers >= h) return h;9 }10}多语言参考
来源 · CC BY-SA 4.0 ↗Java · C++ · Go
这是一组同题正确实现,独立于上方当前动画方法;不同语言可能采用另一种正确策略。
1class Solution {2 public int hIndex(int[] citations) {3 Arrays.sort(citations);4 int n = citations.length;5 for (int h = n; h > 0; --h) {6 if (citations[n - h] >= h) {7 return h;8 }9 }10 return 0;11 }12}交互式算法学习
从执行步骤真正理解 LeetCode 经典 150
本站整理 150 道高频算法面试题和 302 种解法。动画方法同步展示 JavaScript 与 Python;每题另提供 Java、C++ 与 Go 同题参考实现。