78. 子集
描述
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

链接
78. 子集 - 力扣(LeetCode) (leetcode-cn.com)
解法
1 class Solution { 2 List<List<Integer>> res = new ArrayList<>(); 3 Deque<Integer> path = new LinkedList<>(); 4 5 public List<List<Integer>> subsets(int[] nums) { 6 if(nums.length == 0) { 7 return res; 8 } 9 subsetsHelper(nums, 0); 10 return res; 11 } 12 13 public void subsetsHelper(int[] nums, int index) { 14 res.add(new ArrayList<>(path)); 15 if(index > nums.length) return; 16 for (int i = index; i < nums.length; i++) { 17 path.add(nums[i]); 18 subsetsHelper(nums, i + 1); 19 path.removeLast(); 20 } 21 } 22 }
参考
carl

浙公网安备 33010602011771号