subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example, If nums = [1,2,3], a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Show Company Tags Show Tags Show Similar Problems
public class Solution {
public List> subsets(int[] S) {
List
> result = new ArrayList
>();
if(S.length == 0){
return result;
}
Arrays.sort(S);
dfs(S, 0, new ArrayList<Integer>(), result);
return result;
}
public void dfs(int[] s, int index, List<Integer> path, List<List<Integer>> result){
result.add(new ArrayList<Integer>(path));
for(int i = index; i < s.length; i++){
path.add(s[i]);
dfs(s, i+1, path, result);
path.remove(path.size()-1);
}
}
}