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); } } }
posted on 2015-03-10 19:48 zhouzhou0615 阅读(172) 评论(0) 编辑 收藏 举报