LeetCode Top 100 Liked Questions 78. Subsets (Java版; Medium)
题目描述
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
classSolution{public List<List<Integer>>subsets(int[] nums){
List<List<Integer>> list =newArrayList<>();int n = nums.length;if(n==0){return list;}
List<Integer> al =newArrayList<>();backtrace(list, al, nums,0);return list;}privatevoidbacktrace(List<List<Integer>> list, List<Integer> al,int[] nums,int i){//int n = nums.length;if(i==n){
list.add(newArrayList<>(al));return;}//not choosebacktrace(list, al, nums, i+1);//choose
al.add(nums[i]);backtrace(list, al, nums, i+1);
al.remove(al.size()-1);}}