class Solution {
// 保存最终结果
List<List<Integer>> res = new ArrayList<>();
// 暂存结果
List<Integer> path = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
backTracking(nums,0);
return res;
}
public void backTracking(int[] nums,int startIndex){
// 进入一次递归,就要将当前path中得元素保存到最终结果里
res.add(new ArrayList<>(path));
/* 终止条件,可以不写,因为最后一次for循环直接结束,也会结束递归
if(startIndex == nums.length){
return;
}
*/
for(int i=startIndex; i<nums.length; i++){
path.add(nums[i]);
backTracking(nums,i+1);
path.remove(path.size()-1);
}
}
}
运行结果: