查看: 1144| 回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Java/C#] 求大神帮解答lc78 subset中的一个java问题

全局:

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

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

x

请问为什么本题在递归调用必须要用helper(res, new ArrayList<>(each), i + 1, n), 用helper(res, each , i + 1, n)就会出现下面的错误呢?在debug打印时,each还是正确的,为什么在result结果中所有的each都变成了空?谢谢大神们解答,会尽力给大家加米(一天只能加10粒米,一次只能加一粒,谢谢大家)。

Given a set of distinct integers, nums, return all possible subsets (the power set).
-------------这是题目----------------------------------------------
Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
  1. public class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3.     Arrays.sort(nums);
  4.     List<List<Integer>> res = new ArrayList<>();
  5.     List<Integer> each = new ArrayList<>();
  6.     helper(res, each, 0, nums);
  7.     return res;
  8. }
  9. public void helper(List<List<Integer>> res, List<Integer> each, int pos, int[] n) {
  10.    //为了debug,打印一些parameter
  11.     System.out.print(" This is sub-list need to be added ");
  12.     System.out.print(each);
  13.     System.out.print(" pos: ");
  14.     System.out.print(pos);
  15.     System.out.println(" ");
  16.    //debug结束
  17.     if (pos <= n.length) {
  18.         res.add(each);
  19.     }
  20.     for (int i = pos; i < n.length; i++) {
  21.         each.add(n[i]);
  22.         helper(res,each, i + 1, n);
  23.         each.remove(each.size() - 1);
  24.     }
  25.     return;
  26. }
  27. }
复制代码


------------------这是结果和debug打印结果-----------------------
Your input
[1,2,3]
stdout
This is sub-list need to be added [] pos: 0
This is sub-list need to be added [1] pos: 1
This is sub-list need to be added [1, 2] pos: 2
This is sub-list need to be added [1, 2, 3] pos: 3
This is sub-list need to be added [1, 3] pos: 3
This is sub-list need to be added [2] pos: 2
This is sub-list need to be added [2, 3] pos: 3
This is sub-list need to be added [3] pos: 3
Output
[[],[],[],[],[],[],[],[]]
Expected
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]






上一篇:请问:力扣感恩节有优惠吗
下一篇:leetcode 169 测试数据引发的 诡异现象
推荐
jliu 2019-11-24 04:28:59 | 只看该作者
全局:
res add each 仅仅只是存了each这个array list 的reference. 应该是整体深度copy.
res.add(new ArrayList(each));

评分

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

查看全部评分

回复

使用道具 举报

🔗
ogjkfkungosxsf 2019-11-24 11:40:24 | 只看该作者
全局:
因为java是pass by value不是pass by reference,这个问题你刷别的题的时候也会发现的

评分

参与人数 1大米 +1 收起 理由
超人96825 + 1 是的,谢谢

查看全部评分

回复

使用道具 举报

🔗
Mr.Brain 2019-11-24 12:09:44 | 只看该作者
全局:
因为helper(res, each , i + 1, n), each永远都是你主函数中最开始的那个each,这个each第一步递归变成[1]放到res里,然后[1,2]放到res里, [1,2,3]放到res里, 然后[2,3], [2], [3],都放res里,但其实所有res里的each都指向的是同一个最开始的each, 最后一步each变成[], 然后一步步返回,最后每个each都是同一个each那就是空了。所以每一步的放到res里的each都得是一个新的list,它指向的数组不会下一次递归后被修改,。
也就是楼上说的 java都是传值(list就是其引用),你每次都传同一个list的引用,res里所有each都指向同一个list,最后这个list为空,所有list就是空。

评分

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

查看全部评分

回复

使用道具 举报

🔗
337845818 2019-11-24 23:28:52 | 只看该作者
全局:
把地址打印一下不要打印内容

评分

参与人数 1大米 +1 收起 理由
超人96825 + 1 谢谢! 现在明白了

查看全部评分

回复

使用道具 举报

🔗
 楼主| 超人96825 2019-11-25 21:13:53 | 只看该作者
全局:
jliu 发表于 2019-11-24 04:28
res add each 仅仅只是存了each这个array list 的reference. 应该是整体深度copy.
res.add(new ArrayList ...

明白了,谢谢!!
回复

使用道具 举报

🔗
 楼主| 超人96825 2019-11-25 21:14:20 | 只看该作者
全局:
337845818 发表于 2019-11-24 23:28
把地址打印一下不要打印内容
谢谢! 现在明白了
回复

使用道具 举报

🔗
 楼主| 超人96825 2019-11-25 21:19:26 | 只看该作者
全局:
Mr.Brain 发表于 2019-11-24 12:09
因为helper(res, each , i + 1, n), each永远都是你主函数中最开始的那个each,这个each第一步递归变成[1] ...

非常详细的解释,谢谢大家。 现在感觉做题格外注意遇到pass by object reference,还有在solution class的function外定义的primary type或者object, 都有点不会写recursion了;这个可以通过多写提高吗?
回复

使用道具 举报

🔗
 楼主| 超人96825 2019-11-25 21:41:02 | 只看该作者
全局:
337845818 发表于 2019-11-24 23:28
把地址打印一下不要打印内容

请问怎么打印地址? 在debug时可以看到地址,除此还能怎么看到地址呢?谢谢
回复

使用道具 举报

🔗
337845818 2019-11-26 00:07:44 | 只看该作者
全局:
超人96825 发表于 2019-11-25 21:41
请问怎么打印地址? 在debug时可以看到地址,除此还能怎么看到地址呢?谢谢

System.out.println(each.hashCode())
回复

使用道具 举报

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

本版积分规则

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