212. Word Search II
对单词表建立 Trie 树,dfs 暴力搜索。
运行时间 62 ms。
1 | class Solution { |
214. Shortest Palindrome
方法一
问题为求最长的回文前缀。
时间复杂度 O(n),运行时间 42 ms。
1 | class Solution { |
方法二
思路同方法一,使用高效的KMP 算法解决。
时间复杂度 O(n),运行时间 9 ms。
1 | class Solution { |
218. The Skyline Problem
从左到右扫描,记录当前的覆盖情况,使用 STL 库的 multiset 维护当前的最高覆盖。
时间复杂度 O(n * log(n)),运行时间 23 ms。
1 | class Solution { |
224. Basic Calculator
建立两个栈分别存储数字和符号。
时间复杂度 O(n),运行时间 16 ms。
1 | class Solution { |
233. Number of Digit One
按位统计。
时间复杂度 O(1),运行时间 0 ms。
1 | class Solution { |
239. Sliding Window Maximum
维护递减的单调队列。
时间复杂度 O(n),运行时间 69 ms。
1 | class Solution { |
273. Integer to English Words
时间复杂度 O(1),运行时间 3 ms。
1 | class Solution { |
282. Expression Add Operators
dfs 搜索,使用「撤回」的方式处理 * 和 + - 的优先级问题,搜索过程中记录最近一次的 + - 运算。
变量说明:
res– 当前表达式结果last_v– 最近一次+ -运算的数值last_sign– 最近一次+ -运算的符号fml– 当前表达式
运行时间 123 ms。
1 | class Solution { |
295. Find Median from Data Stream
方法一
建立二叉搜索树,维护中位数相关的两元素的指针。
运行时间 213 ms。
1 | class MedianFinder { |
方法二
维护两个堆,一个大根堆和一个小根堆,大根堆存最小的一半元素,小根堆存另一半元素,中位数可由两个堆的堆顶计算得出。
运行时间 176 ms。
1 | class MedianFinder { |
297. Serialize and Deserialize Binary Tree
前序遍历。
运行时间 79 ms。
1 | /** |