LeetCode 经典 150 · 可视化

逐步动画、变量流转与五语言代码参考

#274 · 数组 / 字符串

H 指数

H-Index

测试用例citations = [3,0,6,1,5]
线性序列4 个关键状态
步骤 1建立 0..n 的引用次数桶。count=[1,1,0,1,0,2]
1
0
1
1
0
2
1
3
0
4
2
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}
多语言参考

Java · C++ · Go

来源 · CC BY-SA 4.0 ↗

这是一组同题正确实现,独立于上方当前动画方法;不同语言可能采用另一种正确策略。

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 同题参考实现。

多形态可视化数组、矩阵、DP 表、递归树、树图、链表、栈队列堆、区间和状态机按解法选择。
动画与代码同步当前状态、步骤说明、变量和代码高亮保持一一对应,可逐步播放和回退。
适合面试复习每种方案都给出思路、时间复杂度、空间复杂度、测试用例和 LeetCode 中文原题入口。