学习笔记
public void recur(int level, int param) {
// terminator
// 递归终结条件
if (level > MAX_LEVEL) {
// process result
return;
}
// process current logic
// 处理当前层
process(level, param);
// drill down
// 下探到下一层
recur(level + 1, newParam);
// restore current status
// 清理当前层
}
- 不要人肉进行递归(最大误区)
- 找到最近最简方法,将其拆解成可重复解决的问题(重复子问题)
- 数学归纳法思维
代码模板
private static int divide_conquer(Problem problem,) {
// recursion terminator
if (problem == NULL) {
int res = process_last_result();
return res;
}
// prepare data
data = prepare_dara(problem);
subProblems = split_problem(problem);
// conquer subproblems
res0 = divide_conquer(subProblems[0]);
res1 = divide_conquer(subProblems[1]);
// ...
// process and generate the final result
result = process_result(res0, res1);
// revert the current level states
return result;
}
回溯法采用试错的思想,它尝试分步的去解决一个问题。在分步解决问题的过程中,当它通过尝试发现现有的分步答案不能得到有效的正确的解答的时候,它将取消上一步甚至是上几步的计算,再通过其它的可能的分步解答再次尝试寻找问题的答案。
回溯法通常用最简单的递归方法来实现,在反复重复上述的步骤后可能出现两种情况:
- 找到一个可能存在的正确的答案;
- 在尝试了所有可能的分步方法后宣告该问题没有答案。 在最坏的情况下,回溯法会导致一次复杂度为指数时间的计算。
| 题目编号 |
题目名称 |
难度 |
类型 |
#1 |
#2 |
#3 |
#4 |
#5 |
| 297 |
serialize-and-deserialize-binary-tree |
Hard |
Recursion |
0823/0823 |
0823/0823 |
0824/0824 |
0831/0831 |
|
| 98 |
validate-binary-search-tree |
Medium |
Recursion |
0823/0823 |
0823/0823 |
0824/0825 |
0831/0831 |
|
| 104 |
maximum-depth-of-binary-tree |
Easy |
Recursion |
0823/0823 |
0823/0823 |
0824/0824 |
0831/0831 |
|
| 111 |
minimum-depth-of-binary-tree |
Easy |
Recursion |
0823/0823 |
0823/0823 |
0824/0824 |
0831/0831 |
|
| 题目编号 |
题目名称 |
难度 |
类型 |
#1 |
#2 |
#3 |
#4 |
#5 |
| 236 |
lowest-common-ancestor-of-a-binary-tree |
Medium |
Recursion |
0824/0824 |
0824/0824 |
0825/0825 |
0901/0902 |
|
| 105 |
construct-binary-tree-from-preorder-and-inorder-traversal |
Medium |
Recursion |
0824/0824 |
0824/0824 |
0825/0825 |
0901/0902 |
|
| 77 |
combinations |
Medium |
Recursion |
0824/0824 |
0824/0824 |
0825/0825 |
0901/0902 |
|
| 226 |
invert-binary-tree |
Easy |
Recursion |
0824/0824 |
0824/0824 |
0825/0825 |
0901/0902 |
|
| 题目编号 |
题目名称 |
难度 |
类型 |
#1 |
#2 |
#3 |
#4 |
#5 |
| 51 |
n-queens |
Hard |
Backtracking |
0825/0825 |
0825/0825 |
0826/0826 |
0902/0906 |
|
| 46 |
permutations |
Medium |
Recursion |
0825/0825 |
0825/0825 |
0826/0826 |
0902/0906 |
|
| 47 |
permutations-ii |
Medium |
Recursion |
0825/0825 |
0825/0825 |
0826/0826 |
0902/0906 |
|
| 169 |
majority-element |
Easy |
DivideConquer |
0825/0825 |
0825/0825 |
0826/0826 |
0902/0906 |
|
| 题目编号 |
题目名称 |
难度 |
类型 |
#1 |
#2 |
#3 |
#4 |
#5 |
| 78 |
subsets |
Medium |
Backtracking |
0826/0826 |
0826/0826 |
0827/0827 |
0903/0905 |
|
| 50 |
powx-n |
Medium |
Divide&Conquer |
0826/0826 |
0826/0826 |
0827/0827 |
0903/0905 |
|
| 17 |
letter-combinations-of-a-phone-number |
Medium |
Backtracking |
0826/0826 |
0826/0826 |
0827/0827 |
0903/0905 |
|
| 455 |
assign-cookies |
Easy |
Greedy |
0826/0826 |
0826/0826 |
0827/0827 |
0903/0905 |
|
| 题目编号 |
题目名称 |
难度 |
类型 |
#1 |
#2 |
#3 |
#4 |
#5 |
| 102 |
binary-tree-level-order-traversal |
Medium |
DFS&BFS |
0827/0827 |
0827/0827 |
0828/0829 |
0904/0904 |
|
| 55 |
jump-game |
Medium |
Greedy |
0827/0827 |
0827/0827 |
0828/0828 |
0904/0904 |
|
| 122 |
best-time-to-buy-and-sell-stock-ii |
Easy |
Greedy |
0827/0827 |
0827/0827 |
0828/0828 |
0904/0904 |
|
Have a day off;
| 题目编号 |
题目名称 |
难度 |
类型 |
#1 |
#2 |
#3 |
#4 |
#5 |
| 69 |
sqrtx |
Easy |
BinarySearch |
0829/0829 |
0829/0829 |
0830/0830 |
0906/0906 |
|
| 367 |
valid-perfect-square |
Easy |
BinarySearch |
0829/0829 |
0829/0829 |
0830/0830 |
0906/0906 |
|
| 剑指Offer05 |
替换空格 |
Easy |
Array |
0829/0829 |
0829/0829 |
0830/0830 |
0906/0906 |
|
| 剑指Offer06 |
从尾到头打印链表 |
Easy |
Stack |
0829/0829 |
0829/0829 |
0830/0830 |
0906/0906 |
|