题目
对于这种要求保持原来顺序的,要从下一个坐标开始dfs。
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<>());
dfs(res,new ArrayList<>(),0,nums.length,nums);
return res;
}
public void dfs(List<List<Integer>> res,List<Integer> list,int pos,int n,int[] nums){
if(pos==n) return;
for(int i=pos;i<n;i++){
list.add(nums[i]);
res.add(new ArrayList<>(list));
dfs(res,list,i+1,n,nums);
list.remove(list.size()-1);
}
}
}