《玩转算法面试 从真题到思维全面提升算法思维》
为了面试,更为了提升你的算法思维
一、数组中的问题其实最常见
1. 如何写出正确的程序
- 明确变量的含义
- 明确循环不变量
- 实现二分查找法
- 283. Move Zeroes
- 027. Remove Element
- 026. Remove Duplicates from Sorted Array
- 080. Remove Duplicates from Sorted Array II
2. 基础算法思路的应用
3. 对撞指针
- 167. Two Sum II - Input array is sorted
- 125. Valid Palindrome
- 344. Reverse String
- 345. Reverse Vowels of a String
- 011. Container With Most Water
4. 滑动窗口
- 滑动窗口为[l, r],初始l = 0,r = -1代表滑动窗口中没有值
- 209. Minimum Size Subarray Sum
- 003. Longest Substring Without Repeating Characters
- 438. Find All Anagrams in a String
- 076. Minimum Window Substring
对撞指针和滑动窗口还可以成为双索引技术。
二、查找表相关问题
- 349. Intersection of Two Arrays
- 350. Intersection of Two Arrays II
- 242. Valid Anagram
- 202. Happy Number
- 290. Word Pattern
- 205. Isomorphic Strings
- 451. Sort Characters By Frequency
- 015. 3Sum
- 018. 4Sum
- 016. 3Sum Closest
- 454. 4Sum II
- 049. Group Anagrams
- 447. Number of Boomerangs
- 149. Max Points on a Line
- 219. Contains Duplicate II
- 217. Contains Duplicate
- 220. Contains Duplicate III
三、在链表中穿针引线
- 206. Reverse Linked List
- 092. Reverse Linked List II
- 083. Remove Duplicates from Sorted List
- 086. Partition List
- 328. Odd Even Linked List
- 002. Add Two Numbers
- 445. Add Two Numbers II
- 203. Remove Linked List Elements
- 082. Remove Duplicates from Sorted List II
- 021. Merge Two Sorted Lists
- 024. Swap Nodes in Pairs
- 025. Reverse Nodes in k-Group
- 147. Insertion Sort List
- 148. Sort List
- 237. Delete Node in a Linked List
- 019. Remove Nth Node From End of List
- 061. Rotate List
- 143. Reorder List
- 234. Palindrome Linked List
四、栈、队列、优先队列
1. 栈的基础使用
2. 栈和递归的紧密关系
- 二叉树的遍历可以使用递归、经典非递归、与模拟系统栈3种方法实现
- 144. Binary Tree Preorder Traversal
- 094. Binary Tree Inorder Traversal
- 145. Binary Tree Postorder Traversal
- 341. Flatten Nested List Iterator
3. 队列
- 队列的基本应用-广度优先遍历
- 树的层序遍历
- 102. Binary Tree Level Order Traversal
- 107. Binary Tree Level Order Traversal II
- 103. Binary Tree Zigzag Level Order Traversal
- 199. Binary Tree Right Side View
- BFS和图的最短路径
4. 优先队列
- 优先队列也是一种队列,底层使用堆来实现。统计频率,排序优先队列,O(nlogk)优先队列,O(nlog(n-k))
- 347号题有3种解题思路统计频率,排序优先队列,O(nlogk)优先队列,O(nlog(n-k))
- 347. Top K Frequent Elements
- 23. Merge k Sorted Lists
五、二叉树和递归
1. 二叉树具有天然递归结构
- 104. Maximum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 226. Invert Binary Tree
- 100. Same Tree
- 101. Symmetric Tree
- 222. Count Complete Tree Nodes
- 110. Balanced Binary Tree
2. 注意递归终止条件
- 112. Path Sum
- 404. Sum of Left Leaves
- 257. Binary Tree Paths
- 113. Path Sum II
- 129. Sum Root to Leaf Numbers
3. 更复杂的递归逻辑
4. 二分搜索树中的问题
- 235. Lowest Common Ancestor of a Binary Search Tree
- 098. Validate Binary Search Tree
- 450. Delete Node in a BST
- 108. Convert Sorted Array to Binary Search Tree
- 230. Kth Smallest Element in a BST
- 236. Lowest Common Ancestor of a Binary Tree
经典LCA问题
六、递归和回溯
1. 本质是树形问题
2. 排列问题
3. 组合问题
- 077. 组合
七、动态规划基础
动态规划:将原问题拆解成若干的子问题,同时保存子问题的答案使得每个子问题只求解一次,最终获得原问题的答案。
最优子结构:通过求子问题的最优解,可以获得原问题的最优解。
- 070. Climbing Stairs
- 120. Triangle
- 064. Minimum Path Sum
- 343. Integer Break
- 279. Perfect Squares
- 091. Decode Ways
- 062. Unique Paths
- 063.Unique Paths II
- 198. 打家劫舍
- 213. 打家劫舍 II
- 337. 打家劫舍 III
- 309. 最佳买卖股票时机含冷冻期
- 000. 背包问题
[ ] 416. 分割等和子集
[ ] 300. 最长上升子序列
- 376. 摆动序列
- 000. 最长公共子序列 - LCS
- 000. 迪杰斯特拉 - dijikstra 单源最短路径算法也是动态规划