思路
这个是子集问题,需要把所有节点都加入到res中,而组合和切割是只加入叶子节点。
子集的返回就是当遍历到叶子节点时就返回。
代码
class Solution {
List<Integer> path = new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
back(nums,0);
return res;
}
public void back(int[] nums,int startIndex){
res.add(new ArrayList(path));
if(startIndex >= nums.length){
return;
}
for(int i = startIndex;i<nums.length;i++){
path.add(nums[i]);
back(nums,i+1);
path.remove(path.size()-1);
}
}
}