Skip to content

Commit 32d44ac

Browse files
committed
week03 first commit
1 parent 55b8a79 commit 32d44ac

4 files changed

Lines changed: 120 additions & 1 deletion

File tree

Week03/NOTE.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
学习笔记
1+
学习笔记
2+
###递归的实现,特性以及思维要点
3+
#####递归-循环,字节码上看是相似的
4+
向下递归到不同的层,向上又回到原来一层
5+
用参数来传递不同层之间的变量
6+
经典递归,计算n!
7+
递归代码模板
8+
1. 递归终结条件 recursion terminator
9+
public void recur(int level,int param){
10+
if level > MAX_LEVEL
11+
process_result
12+
return;
13+
}
14+
2. 处理当前层逻辑 process logic in current level
15+
process(level,param)
16+
3. 下探到下一层 drill down
17+
recur(level+1,newParam)
18+
4. 清理当前层
19+
20+
// Java
21+
public void recur(int level, int param) {
22+
// terminator
23+
if (level > MAX_LEVEL) {
24+
// process result
25+
return;
26+
}
27+
// process current logic
28+
process(level, param);
29+
// drill down
30+
recur( level: level + 1, newParam);
31+
// restore current status
32+
33+
}
34+
#####递归思维要点
35+
1.不要进行人肉递归(最大误区)
36+
2.找到最近最简方法,将其拆解成可重复解决的问题(重复子问题)
37+
3.数学归纳法的思维
38+
#####分治回溯就是一种特殊的递归或者是较为复杂的递归
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.myhomework;
2+
3+
public class LowestCommonAncestor {
4+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
5+
if (root == null || root == p || root == q) return root;
6+
TreeNode left = lowestCommonAncestor(root.left, p, q);
7+
TreeNode right = lowestCommonAncestor(root.right, p, q);
8+
//当left和right同时为空,说明左右子树都不包含p,q,返回null
9+
if (left == null && right == null) return null;
10+
//p,q其中一个在root的右子树中,此时right指向 p(假设为p);
11+
//p,q两节点都在root的右子树中,此时的right指向最近公共祖先节点 ;
12+
13+
if (left == null) return right;
14+
if (right == null) return left;
15+
//当左右子树都不为空,说明p, q分列在root的异侧(分别在左/右子树)
16+
//因此root为最近公共祖先,返回root
17+
return root;
18+
19+
}
20+
}

Week03/com/myhomework/Permute.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.myhomework;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Permute {
7+
public List<List<Integer>> permute(int[] nums) {
8+
// 首先是特判
9+
int len = nums.length;
10+
// 使用一个动态数组保存所有可能的全排列
11+
List<List<Integer>> res = new ArrayList<>();
12+
13+
if (len == 0) {
14+
return res;
15+
}
16+
17+
boolean[] used = new boolean[len];
18+
List<Integer> path = new ArrayList<>();
19+
20+
dfs(nums, len, 0, path, used, res);
21+
return res;
22+
}
23+
private void dfs(int[] nums, int len, int depth,
24+
List<Integer> path, boolean[] used,
25+
List<List<Integer>> res) {
26+
if (depth == len) {
27+
res.add(new ArrayList<>(path));
28+
return;
29+
}
30+
31+
for (int i = 0; i < len; i++) {
32+
if (!used[i]) {
33+
path.add(nums[i]);
34+
used[i] = true;
35+
36+
dfs(nums, len, depth + 1, path, used, res);
37+
// 注意:这里是状态重置,是从深层结点回到浅层结点的过程,代码在形式上和递归之前是对称的
38+
used[i] = false;
39+
path.remove(path.size() - 1);
40+
}
41+
}
42+
}
43+
public static void main(String[] args) {
44+
int[] nums = {1, 2, 3};
45+
Permute test = new Permute();
46+
List<List<Integer>> lists = test.permute(nums);
47+
System.out.println(lists);
48+
}
49+
50+
51+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.myhomework;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode(int x) {
9+
this.val=x;
10+
}
11+
}

0 commit comments

Comments
 (0)