给定一个含不同整数的集合,返回其所有的子集
public class Solution { /** * @param nums: A set of numbers * @return: A list of lists */ public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); if (nums == null) { return null; } Arrays.sort(nums); List<Integer> item = new ArrayList<>(); for (int i = 0; i <= nums.length; i++) { item.clear(); dfs(nums, 0, i, item, result); } return result; } public void dfs(int[] nums, int start, int size, List<Integer> item, List<List<Integer>> result) { if (item.size() == size) { result.add(new ArrayList<>(item)); return; } for (int i = start; i < nums.length; i++) { item.add(nums[i]); dfs(nums, i + 1, size, item, result); item.remove(item.size() - 1); } } }