📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2372| 回复: 11
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 46 permutations backtracking请教

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
请教一下各位大神,手动写test case的时候,对于如下递归部分的回溯很糊涂,不明白是怎样从123返回12然后变成13的。请不吝赐教

  1. public void dfs(List<List<Integer>> res, List<Integer> path, int[] nums){
  2.                 if(path.size() == nums.length){
  3.                         res.add(new ArrayList<>(path));
  4.                         return;
  5.                 }
  6.                
  7.                 for(int i = 0; i < nums.length; i++){
  8.                         if(path.contains(nums[i])) continue;
  9.                     path.add(nums[i]);       
  10.                         dfs(res, path, nums);
  11.                         path.remove(path.size() - 1);
  12.                 }
复制代码


上一篇:以后可以不用买premium了
下一篇:leetcode 用safari 打开看不到 checkmark
推荐
xenophon 2020-1-13 12:14:45 | 只看该作者
全局:
小水 发表于 2020-1-13 11:46
谢谢回答!还想问一下,index i什么时候变成2的?去掉2以后,loop往下走,在我看来,index i还是1,加的 ...

我是这么理解的,如果不对轻喷lol。

比如1,2,3. 这时候i = 2, 然后进recursion,hit length之后回到上一层,这时候 i 还是2,但是要去掉最后一个数字3,那么list就是1,2. 然后整个method走完了,再回到上一层,这时候i = 1,继续执行path.remove(),那么list就是1,然后继续loop,加的就是3了。然后再进一次recursion,那么就是整个loop再来一遍,把没有加到的2加进去。

评分

参与人数 1大米 +1 收起 理由
小水 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

推荐
xenophon 2020-1-13 11:30:12 | 只看该作者
全局:
path.remove(path.size() - 1) 这句话就是把list最后一个去掉了,然后再add下一个。比如1,2:移掉2,下一个loop就是+3了

评分

参与人数 1大米 +1 收起 理由
小水 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

全局:
我前几天写过一个这个的讲解,可以参考一下。 https://www.1point3acres.com/bbs/thread-583166-1-1.html

评分

参与人数 1大米 +1 收起 理由
小水 + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小水 2020-1-13 11:00:54 | 只看该作者
全局:
不知道小帅 发表于 2020-1-13 10:42
我前几天写过一个这个的讲解,可以参考一下。 https://www.1point3acres.com/bbs/thread-583166-1-1.html
track = {1, 2, 3} 触发了base case 的条件,把第一个permutation加入ans list。然后第一个×的地方,我们执行remove操作,
这时候track只剩{1, 2}, 然后沿着这条线继续走,到了下一个×,这时候移除2。我们的track只剩{1}。然后经过后两个红点,得到{1, 3, 2}。

谢谢楼主,还想请教一下,在code实现上,是怎样做到移除2,剩{1}的后,下一个加入的是3而不是依次又加入2的呢?recursion那步出来后,i值是保留recursion里的值还是上一层循环里的值
回复

使用道具 举报

全局:
小水 发表于 2020/01/13 11:00:54

谢谢楼主,还想请教一下,在code实现上,是怎样做到移除2,剩{1}的后,下一个加入的是3而不是依次又加入2的呢?r...
就是在我画的那个图里面啊,在dfs之前加入,之后删除啊。到了1的时候,for 循环进入下一。
回复

使用道具 举报

🔗
 楼主| 小水 2020-1-13 11:46:41 | 只看该作者
全局:
xenophon 发表于 2020-1-13 11:30
path.remove(path.size() - 1) 这句话就是把list最后一个去掉了,然后再add下一个。比如1,2:移掉2,下一 ...

谢谢回答!还想问一下,index i什么时候变成2的?去掉2以后,loop往下走,在我看来,index i还是1,加的还是nums[1]= 2
回复

使用道具 举报

全局:
本帖最后由 不知道小帅 于 2020-1-13 12:14 编辑

我不知道这个图片能不能解答楼主的疑问。我画x的地方就是不满足path.contains(nums[i]),所以被剪枝。

c9c399d6-c47d-4b84-9afe-aa320a9baee3.jpg (29.96 KB, 下载次数: 7)

c9c399d6-c47d-4b84-9afe-aa320a9baee3.jpg

评分

参与人数 1大米 +1 收起 理由
小水 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
magicsets 2020-1-13 15:40:36 | 只看该作者
全局:
下面这份代码用一个“TreeSerializer“的工具class将dfs()的执行过程打印出来了,楼主可以先看看下面的贴出来的输出,也可以自己再加一些debug信息,应该比较容易弄清楚执行过程。

代码:
  1. import java.util.ArrayList;
  2. import java.util.List;

  3. public class Main {
  4.   public void dfs(List<List<Integer>> res, List<Integer> path, int[] nums,
  5.                   TreeSerializer frameTracker) {
  6.     if (path.size() == nums.length) {
  7.       frameTracker.addString("done one permutation: " + path);
  8.       res.add(new ArrayList<>(path));
  9.       return;
  10.     }
  11.    
  12.     for (int i = 0; i < nums.length; i++) {
  13.       frameTracker.addString("LOOP i = " + i + ", path = " + path);
  14.       TreeSerializer loopTracker = frameTracker.addChild();
  15.       
  16.       if (path.contains(nums[i])) {
  17.         loopTracker.addString("path contains nums[i] which is " + nums[i] + ", continue");
  18.         continue;
  19.       }

  20.       path.add(nums[i]);

  21.       loopTracker.addString("now add nums[i] into path, result path = " + path);
  22.       loopTracker.addString("Calling dfs() with argument path = " + path +
  23.                             ", recursion depth = " + path.size());

  24.       dfs(res, path, nums, loopTracker.addChild());

  25.       path.remove(path.size() - 1);

  26.       loopTracker.addString("now remove nums[i] from path, result path = " + path);
  27.     }
  28.   }

  29.   public static void main(String[] args) {
  30.     TreeSerializer frameTracker = new TreeSerializer(0);
  31.     frameTracker.addString("Calling dfs() with argument path = [], recursion depth = 0");
  32.     new Main().dfs(new ArrayList<>(), new ArrayList<>(),
  33.                    new int[] { 1, 2, 3 }, frameTracker.addChild());
  34.     System.out.println(frameTracker.toString());
  35.   }
  36. }

  37. // 用于打印递归调用的树形结构
  38. class TreeSerializer {
  39.   private List<Object> items = new ArrayList<>();
  40.   private int nestedLevel;
  41.   
  42.   TreeSerializer(int nestedLevel) {
  43.     this.nestedLevel = nestedLevel;
  44.   }
  45.   
  46.   public void addString(String value) {
  47.     items.add(value);
  48.   }
  49.   
  50.   public TreeSerializer addChild() {
  51.     TreeSerializer child = new TreeSerializer(nestedLevel + 1);
  52.     items.add(child);
  53.     return child;
  54.   }
  55.   
  56.   private void serializeToString(StringBuilder sb, String prefix) {
  57.     int numItems = items.size();
  58.     for (int i = 0; i < numItems; ++i) {
  59.       Object item = items.get(i);
  60.       if (item instanceof String) {
  61.         sb.append(prefix + "+- " + item + "\n");
  62.         continue;
  63.       }
  64.       ((TreeSerializer) item).serializeToString(
  65.           sb, prefix + (i == numItems - 1 ? "  " : "| "));
  66.     }
  67.   }
  68.   
  69.   @Override
  70.   public String toString() {
  71.     StringBuilder sb = new StringBuilder();
  72.     serializeToString(sb, "");
  73.     return sb.toString();
  74.   }  
  75. }
复制代码


输出:
  1. +- Calling dfs() with argument path = [], recursion depth = 0
  2.   +- LOOP i = 0, path = []
  3.   | +- now add nums[i] into path, result path = [1]
  4.   | +- Calling dfs() with argument path = [1], recursion depth = 1
  5.   | | +- LOOP i = 0, path = [1]
  6.   | | | +- path contains nums[i] which is 1, continue
  7.   | | +- LOOP i = 1, path = [1]
  8.   | | | +- now add nums[i] into path, result path = [1, 2]
  9.   | | | +- Calling dfs() with argument path = [1, 2], recursion depth = 2
  10.   | | | | +- LOOP i = 0, path = [1, 2]
  11.   | | | | | +- path contains nums[i] which is 1, continue
  12.   | | | | +- LOOP i = 1, path = [1, 2]
  13.   | | | | | +- path contains nums[i] which is 2, continue
  14.   | | | | +- LOOP i = 2, path = [1, 2]
  15.   | | | |   +- now add nums[i] into path, result path = [1, 2, 3]
  16.   | | | |   +- Calling dfs() with argument path = [1, 2, 3], recursion depth = 3
  17.   | | | |   | +- done one permutation: [1, 2, 3]
  18.   | | | |   +- now remove nums[i] from path, result path = [1, 2]
  19.   | | | +- now remove nums[i] from path, result path = [1]
  20.   | | +- LOOP i = 2, path = [1]
  21.   | |   +- now add nums[i] into path, result path = [1, 3]
  22.   | |   +- Calling dfs() with argument path = [1, 3], recursion depth = 2
  23.   | |   | +- LOOP i = 0, path = [1, 3]
  24.   | |   | | +- path contains nums[i] which is 1, continue
  25.   | |   | +- LOOP i = 1, path = [1, 3]
  26.   | |   | | +- now add nums[i] into path, result path = [1, 3, 2]
  27.   | |   | | +- Calling dfs() with argument path = [1, 3, 2], recursion depth = 3
  28.   | |   | | | +- done one permutation: [1, 3, 2]
  29.   | |   | | +- now remove nums[i] from path, result path = [1, 3]
  30.   | |   | +- LOOP i = 2, path = [1, 3]
  31.   | |   |   +- path contains nums[i] which is 3, continue
  32.   | |   +- now remove nums[i] from path, result path = [1]
  33.   | +- now remove nums[i] from path, result path = []
  34.   +- LOOP i = 1, path = []
  35.   | +- now add nums[i] into path, result path = [2]
  36.   | +- Calling dfs() with argument path = [2], recursion depth = 1
  37.   | | +- LOOP i = 0, path = [2]
  38.   | | | +- now add nums[i] into path, result path = [2, 1]
  39.   | | | +- Calling dfs() with argument path = [2, 1], recursion depth = 2
  40.   | | | | +- LOOP i = 0, path = [2, 1]
  41.   | | | | | +- path contains nums[i] which is 1, continue
  42.   | | | | +- LOOP i = 1, path = [2, 1]
  43.   | | | | | +- path contains nums[i] which is 2, continue
  44.   | | | | +- LOOP i = 2, path = [2, 1]
  45.   | | | |   +- now add nums[i] into path, result path = [2, 1, 3]
  46.   | | | |   +- Calling dfs() with argument path = [2, 1, 3], recursion depth = 3
  47.   | | | |   | +- done one permutation: [2, 1, 3]
  48.   | | | |   +- now remove nums[i] from path, result path = [2, 1]
  49.   | | | +- now remove nums[i] from path, result path = [2]
  50.   | | +- LOOP i = 1, path = [2]
  51.   | | | +- path contains nums[i] which is 2, continue
  52.   | | +- LOOP i = 2, path = [2]
  53.   | |   +- now add nums[i] into path, result path = [2, 3]
  54.   | |   +- Calling dfs() with argument path = [2, 3], recursion depth = 2
  55.   | |   | +- LOOP i = 0, path = [2, 3]
  56.   | |   | | +- now add nums[i] into path, result path = [2, 3, 1]
  57.   | |   | | +- Calling dfs() with argument path = [2, 3, 1], recursion depth = 3
  58.   | |   | | | +- done one permutation: [2, 3, 1]
  59.   | |   | | +- now remove nums[i] from path, result path = [2, 3]
  60.   | |   | +- LOOP i = 1, path = [2, 3]
  61.   | |   | | +- path contains nums[i] which is 2, continue
  62.   | |   | +- LOOP i = 2, path = [2, 3]
  63.   | |   |   +- path contains nums[i] which is 3, continue
  64.   | |   +- now remove nums[i] from path, result path = [2]
  65.   | +- now remove nums[i] from path, result path = []
  66.   +- LOOP i = 2, path = []
  67.     +- now add nums[i] into path, result path = [3]
  68.     +- Calling dfs() with argument path = [3], recursion depth = 1
  69.     | +- LOOP i = 0, path = [3]
  70.     | | +- now add nums[i] into path, result path = [3, 1]
  71.     | | +- Calling dfs() with argument path = [3, 1], recursion depth = 2
  72.     | | | +- LOOP i = 0, path = [3, 1]
  73.     | | | | +- path contains nums[i] which is 1, continue
  74.     | | | +- LOOP i = 1, path = [3, 1]
  75.     | | | | +- now add nums[i] into path, result path = [3, 1, 2]
  76.     | | | | +- Calling dfs() with argument path = [3, 1, 2], recursion depth = 3
  77.     | | | | | +- done one permutation: [3, 1, 2]
  78.     | | | | +- now remove nums[i] from path, result path = [3, 1]
  79.     | | | +- LOOP i = 2, path = [3, 1]
  80.     | | |   +- path contains nums[i] which is 3, continue
  81.     | | +- now remove nums[i] from path, result path = [3]
  82.     | +- LOOP i = 1, path = [3]
  83.     | | +- now add nums[i] into path, result path = [3, 2]
  84.     | | +- Calling dfs() with argument path = [3, 2], recursion depth = 2
  85.     | | | +- LOOP i = 0, path = [3, 2]
  86.     | | | | +- now add nums[i] into path, result path = [3, 2, 1]
  87.     | | | | +- Calling dfs() with argument path = [3, 2, 1], recursion depth = 3
  88.     | | | | | +- done one permutation: [3, 2, 1]
  89.     | | | | +- now remove nums[i] from path, result path = [3, 2]
  90.     | | | +- LOOP i = 1, path = [3, 2]
  91.     | | | | +- path contains nums[i] which is 2, continue
  92.     | | | +- LOOP i = 2, path = [3, 2]
  93.     | | |   +- path contains nums[i] which is 3, continue
  94.     | | +- now remove nums[i] from path, result path = [3]
  95.     | +- LOOP i = 2, path = [3]
  96.     |   +- path contains nums[i] which is 3, continue
  97.     +- now remove nums[i] from path, result path = []
复制代码

评分

参与人数 1大米 +1 收起 理由
小水 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小水 2020-1-14 07:49:40 | 只看该作者
全局:
本帖最后由 小水 于 2020-1-14 08:06 编辑
xenophon 发表于 2020-1-13 12:14
我是这么理解的,如果不对轻喷lol。

比如1,2,3. 这时候i = 2, 然后进recursion,hit length之后回 ...

谢谢解答!很make sense,昨天纠结了好几个小时都没想明白,早来跟大神请教就好了TT
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表