清理cosbench工具所佔用磁碟空間的小指令碼
阿新 • • 發佈:2020-11-03
子集
題目:
給定一組不含重複元素的整數陣列 nums,返回該陣列所有可能的子集(冪集)。
說明:解集不能包含重複的子集。
示例:
輸入: nums = [1,2,3]
輸出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
解題思路:用回溯演算法解決,在紙上畫出整個子集[[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]可以總結出規律
class Solution { private List<List<Integer>> ans = new ArrayList(); // private Set<List<Integer>> set = new HashSet(); public List<List<Integer>> subsets(int[] nums) { dfs(nums, new ArrayList<Integer>(), 0); return ans; } private void dfs(int[] nums, List<Integer> list, int cur) { ans.add(new ArrayList<>(list)); for (int j = cur; j < nums.length; j++) { list.add(nums[j]); dfs(nums, list, j + 1); list.remove(list.size() - 1); } } }