LeetCode 28 找出字符串中第一个匹配项的下标
一道表面简单、实则把 KMP 请上桌的字符串匹配题。本文记录暴力解法与 KMP 模板,并把 next 数组的核心逻辑拆成人话。
1719 words
|
9 minutes
Cover Image of the Post
LeetCode 215 数组中的第K个最大元素
这题可以直接排序,也可以用容量为 K 的最小堆,更进阶的做法是快速选择。本文重点记录最小堆与快速选择的思路和易错点。
1649 words
|
8 minutes
Cover Image of the Post
LeetCode 347 前K个高频元素
这题的核心是先统计频率,再用一个容量为 K 的小顶堆维护当前前 K 个高频元素。本文记录 Counter + 小顶堆的标准解法。
1224 words
|
6 minutes
Cover Image of the Post
LeetCode 295 数据流的中位数
这题的核心是双堆法:大顶堆维护较小的一半,小顶堆维护较大的一半,并始终保持大顶堆数量大于或等于小顶堆。
1612 words
|
8 minutes
Cover Image of the Post
LeetCode 121 买卖股票的最佳时机
这题的核心是贪心:遍历过程中维护前面的最低价格,再用当天价格尝试更新最大利润。
1364 words
|
7 minutes
Cover Image of the Post
Leetcode Hot 100 括号生成
Leetcode Hot 100 经典回溯剪枝题:括号生成。本文用左右括号数量约束讲清楚如何在构造过程中直接剪掉非法前缀。
701 words
|
4 minutes
Cover Image of the Post
Leetcode Hot 100 分割回文串
Leetcode Hot 100 回溯经典题:分割回文串。本文从切割视角拆解如何枚举每一段的结束位置,并用回溯收集所有合法的回文分割方案。
1486 words
|
7 minutes
Cover Image of the Post
Leetcode Hot 100 实现 Trie(前缀树)
Leetcode Hot 100 字典树模板题:实现 Trie(前缀树)。本文整理节点设计、插入、查找、前缀判断三步走,顺手讲清楚 search 和 startsWith 的区别。
1510 words
|
8 minutes
Cover Image of the Post