Subsets

Subsets

问题:

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

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

思路:

  DFS + 回溯

我的代码:

public class Solution {
    public List<List<Integer>> subsets(int[] S) {
        if(S == null || S.length == 0)  return rst;
        List<Integer> list = new ArrayList<Integer>();
        Arrays.sort(S);
        helper(list, S, 0);
        return rst;
    }
    private List<List<Integer>> rst = new ArrayList<List<Integer>>();
    public void helper(List<Integer> list, int[] candidates, int start)
    {
        rst.add(new ArrayList(list));
        for(int i = start ; i < candidates.length; i++)
        {
            list.add(candidates[i]);
            helper(list, candidates, i + 1);
            list.remove(list.size() - 1);
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/sunshisonghit/p/4328264.html