Level:
??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],
[]
]
思路分析:
?求子组合问题,这种题的解法都是回溯进行遍历,将满足要求的解返回。
代码:
public class Solution{
public List<List<Integer>>subsets(int nums[]){
List<List<Integer>>res=new ArrayList<>();
if(nums==null||nums.length==0)
return res;
Arrays.sort(nums); //进行排序,按要求顺序输出
back(res,nums,new ArrayList<>(),0);
return res;
}
public void back(List<List<Integer>>res,int []nums,List<Integer>list,int start){
res.add(new ArrayList<>(list));
for(int i=start;i<nums.length;i++){
list.add(nums[i]);
back(res,nums,list,i+1);
list.remove(list.size()-1);
}
}
}
原文地址:https://www.cnblogs.com/yjxyy/p/11074890.html
时间: 2024-10-25 02:46:04