一些要注意的点:
怪不得 复制工作容器副本( LinkedList t = new LinkedList<>(path);)会报错(报错信息如下:Cannot invoke “java.util.Collection.toArray()” because “c” is null);
!!! 原来是因为我没有为 path 初始化 new 一片空间出来
public void dfs(int[] nums, int i,LinkedList s){ // 也可以写成LinkedList s
// 一开始 res 里面始终为空的原因是:我想用字符串作为工作容器,最后往res里面存的却是path,所以一开始一定要只确定一种方案,并且消除其他方案的影响
//真是傻逼,你用的字符串,你放弃了用path这个容器啊
dfs(nums,0,path); // String 不好转 LinkedList, 不如直接用 LinkedList
题目如下:
#代码如下:
class Solution {
Set<List<Integer>> res ;
LinkedList<Integer> path ;
public List<List<Integer>> subsets(int[] nums) {
res = new HashSet<>();
path= new LinkedList<>();//怪不得 复制工作容器副本( LinkedList<Integer> t = new LinkedList<>(path);)会报错(报错信息如下:Cannot invoke "java.util.Collection.toArray()" because "c" is null);
// 原来是因为我没有为 path 初始化 new 一片空间出来
dfs(nums,0,path); // String 不好转 LinkedList, 不如直接用 LinkedList
return new LinkedList<>(res);
}
public void dfs(int[] nums, int i,LinkedList s){ // 也可以写成LinkedList<Integer> s
if(i == nums.length){
LinkedList<Integer> t = new LinkedList <>(path);
// 一开始 res 里面始终为空的原因是:我想用字符串作为工作容器,最后往res里面存的却是path,所以一开始一定要只确定一种方案,并且消除其他方案的影响
//真是傻逼,你用的字符串,你放弃了用path这个容器啊
res.add(t);
return;
}
path.add(nums[i]);
dfs(nums,i+1, path);
path.removeLast();
dfs(nums, i+1, s);
}
}