Description
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example
1 2 3 4 5 6 7 8 9 10 11 12
| Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
|
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public void backtrack(List<List<Integer>> res, List<Integer> temp, int[] nums, int start){ res.add(new ArrayList<Integer>(temp)); for (int i = start; i < nums.length; i++){ temp.add(nums[i]); backtrack(res, temp, nums, i + 1); temp.remove(temp.size()-1); } } public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); backtrack(res, new ArrayList<Integer>(), nums, 0); return res; } }
|