高级农民
- 积分
- 2737
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-6-18
- 最后登录
- 1970-1-1
|
下面这份代码用一个“TreeSerializer“的工具class将dfs()的执行过程打印出来了,楼主可以先看看下面的贴出来的输出,也可以自己再加一些debug信息,应该比较容易弄清楚执行过程。
代码:
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- public void dfs(List<List<Integer>> res, List<Integer> path, int[] nums,
- TreeSerializer frameTracker) {
- if (path.size() == nums.length) {
- frameTracker.addString("done one permutation: " + path);
- res.add(new ArrayList<>(path));
- return;
- }
-
- for (int i = 0; i < nums.length; i++) {
- frameTracker.addString("LOOP i = " + i + ", path = " + path);
- TreeSerializer loopTracker = frameTracker.addChild();
-
- if (path.contains(nums[i])) {
- loopTracker.addString("path contains nums[i] which is " + nums[i] + ", continue");
- continue;
- }
- path.add(nums[i]);
- loopTracker.addString("now add nums[i] into path, result path = " + path);
- loopTracker.addString("Calling dfs() with argument path = " + path +
- ", recursion depth = " + path.size());
- dfs(res, path, nums, loopTracker.addChild());
- path.remove(path.size() - 1);
- loopTracker.addString("now remove nums[i] from path, result path = " + path);
- }
- }
- public static void main(String[] args) {
- TreeSerializer frameTracker = new TreeSerializer(0);
- frameTracker.addString("Calling dfs() with argument path = [], recursion depth = 0");
- new Main().dfs(new ArrayList<>(), new ArrayList<>(),
- new int[] { 1, 2, 3 }, frameTracker.addChild());
- System.out.println(frameTracker.toString());
- }
- }
- // 用于打印递归调用的树形结构
- class TreeSerializer {
- private List<Object> items = new ArrayList<>();
- private int nestedLevel;
-
- TreeSerializer(int nestedLevel) {
- this.nestedLevel = nestedLevel;
- }
-
- public void addString(String value) {
- items.add(value);
- }
-
- public TreeSerializer addChild() {
- TreeSerializer child = new TreeSerializer(nestedLevel + 1);
- items.add(child);
- return child;
- }
-
- private void serializeToString(StringBuilder sb, String prefix) {
- int numItems = items.size();
- for (int i = 0; i < numItems; ++i) {
- Object item = items.get(i);
- if (item instanceof String) {
- sb.append(prefix + "+- " + item + "\n");
- continue;
- }
- ((TreeSerializer) item).serializeToString(
- sb, prefix + (i == numItems - 1 ? " " : "| "));
- }
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- serializeToString(sb, "");
- return sb.toString();
- }
- }
复制代码
输出:
- +- Calling dfs() with argument path = [], recursion depth = 0
- +- LOOP i = 0, path = []
- | +- now add nums[i] into path, result path = [1]
- | +- Calling dfs() with argument path = [1], recursion depth = 1
- | | +- LOOP i = 0, path = [1]
- | | | +- path contains nums[i] which is 1, continue
- | | +- LOOP i = 1, path = [1]
- | | | +- now add nums[i] into path, result path = [1, 2]
- | | | +- Calling dfs() with argument path = [1, 2], recursion depth = 2
- | | | | +- LOOP i = 0, path = [1, 2]
- | | | | | +- path contains nums[i] which is 1, continue
- | | | | +- LOOP i = 1, path = [1, 2]
- | | | | | +- path contains nums[i] which is 2, continue
- | | | | +- LOOP i = 2, path = [1, 2]
- | | | | +- now add nums[i] into path, result path = [1, 2, 3]
- | | | | +- Calling dfs() with argument path = [1, 2, 3], recursion depth = 3
- | | | | | +- done one permutation: [1, 2, 3]
- | | | | +- now remove nums[i] from path, result path = [1, 2]
- | | | +- now remove nums[i] from path, result path = [1]
- | | +- LOOP i = 2, path = [1]
- | | +- now add nums[i] into path, result path = [1, 3]
- | | +- Calling dfs() with argument path = [1, 3], recursion depth = 2
- | | | +- LOOP i = 0, path = [1, 3]
- | | | | +- path contains nums[i] which is 1, continue
- | | | +- LOOP i = 1, path = [1, 3]
- | | | | +- now add nums[i] into path, result path = [1, 3, 2]
- | | | | +- Calling dfs() with argument path = [1, 3, 2], recursion depth = 3
- | | | | | +- done one permutation: [1, 3, 2]
- | | | | +- now remove nums[i] from path, result path = [1, 3]
- | | | +- LOOP i = 2, path = [1, 3]
- | | | +- path contains nums[i] which is 3, continue
- | | +- now remove nums[i] from path, result path = [1]
- | +- now remove nums[i] from path, result path = []
- +- LOOP i = 1, path = []
- | +- now add nums[i] into path, result path = [2]
- | +- Calling dfs() with argument path = [2], recursion depth = 1
- | | +- LOOP i = 0, path = [2]
- | | | +- now add nums[i] into path, result path = [2, 1]
- | | | +- Calling dfs() with argument path = [2, 1], recursion depth = 2
- | | | | +- LOOP i = 0, path = [2, 1]
- | | | | | +- path contains nums[i] which is 1, continue
- | | | | +- LOOP i = 1, path = [2, 1]
- | | | | | +- path contains nums[i] which is 2, continue
- | | | | +- LOOP i = 2, path = [2, 1]
- | | | | +- now add nums[i] into path, result path = [2, 1, 3]
- | | | | +- Calling dfs() with argument path = [2, 1, 3], recursion depth = 3
- | | | | | +- done one permutation: [2, 1, 3]
- | | | | +- now remove nums[i] from path, result path = [2, 1]
- | | | +- now remove nums[i] from path, result path = [2]
- | | +- LOOP i = 1, path = [2]
- | | | +- path contains nums[i] which is 2, continue
- | | +- LOOP i = 2, path = [2]
- | | +- now add nums[i] into path, result path = [2, 3]
- | | +- Calling dfs() with argument path = [2, 3], recursion depth = 2
- | | | +- LOOP i = 0, path = [2, 3]
- | | | | +- now add nums[i] into path, result path = [2, 3, 1]
- | | | | +- Calling dfs() with argument path = [2, 3, 1], recursion depth = 3
- | | | | | +- done one permutation: [2, 3, 1]
- | | | | +- now remove nums[i] from path, result path = [2, 3]
- | | | +- LOOP i = 1, path = [2, 3]
- | | | | +- path contains nums[i] which is 2, continue
- | | | +- LOOP i = 2, path = [2, 3]
- | | | +- path contains nums[i] which is 3, continue
- | | +- now remove nums[i] from path, result path = [2]
- | +- now remove nums[i] from path, result path = []
- +- LOOP i = 2, path = []
- +- now add nums[i] into path, result path = [3]
- +- Calling dfs() with argument path = [3], recursion depth = 1
- | +- LOOP i = 0, path = [3]
- | | +- now add nums[i] into path, result path = [3, 1]
- | | +- Calling dfs() with argument path = [3, 1], recursion depth = 2
- | | | +- LOOP i = 0, path = [3, 1]
- | | | | +- path contains nums[i] which is 1, continue
- | | | +- LOOP i = 1, path = [3, 1]
- | | | | +- now add nums[i] into path, result path = [3, 1, 2]
- | | | | +- Calling dfs() with argument path = [3, 1, 2], recursion depth = 3
- | | | | | +- done one permutation: [3, 1, 2]
- | | | | +- now remove nums[i] from path, result path = [3, 1]
- | | | +- LOOP i = 2, path = [3, 1]
- | | | +- path contains nums[i] which is 3, continue
- | | +- now remove nums[i] from path, result path = [3]
- | +- LOOP i = 1, path = [3]
- | | +- now add nums[i] into path, result path = [3, 2]
- | | +- Calling dfs() with argument path = [3, 2], recursion depth = 2
- | | | +- LOOP i = 0, path = [3, 2]
- | | | | +- now add nums[i] into path, result path = [3, 2, 1]
- | | | | +- Calling dfs() with argument path = [3, 2, 1], recursion depth = 3
- | | | | | +- done one permutation: [3, 2, 1]
- | | | | +- now remove nums[i] from path, result path = [3, 2]
- | | | +- LOOP i = 1, path = [3, 2]
- | | | | +- path contains nums[i] which is 2, continue
- | | | +- LOOP i = 2, path = [3, 2]
- | | | +- path contains nums[i] which is 3, continue
- | | +- now remove nums[i] from path, result path = [3]
- | +- LOOP i = 2, path = [3]
- | +- path contains nums[i] which is 3, continue
- +- now remove nums[i] from path, result path = []
复制代码 |
|