Subsets(子集)

问题

Given a set of distinct integers, return all possible subsets.

Notice

Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
Have you met this question in a real interview? Yes
Example
If S = [1,2,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

分析

请参阅 数字组合

代码

public class Solution {
   
   /*
    * @param nums: A set of numbers
    * @return: A list of lists
    */
   public List> subsets(int[] nums) {
       // write your code here
       List> res=new ArrayList();
       Arrays.sort(nums);
       tree(res,new LinkedList(),nums,0);
       return res;
   }
   private void tree(List> res,LinkedList list,int[] nums,int index){
       res.add(new ArrayList(list));
       for(int i=index;i

你可能感兴趣的:(Subsets(子集))