题意
生成子集(即所有组合)
题解
使用dfs。
代码
public class Solution {
ArrayList<List<Integer>> lists = new ArrayList<List<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
void combine(int[] nums, int pos, int n)
{
if(pos == n)
{
if(!lists.contains(temp))
lists.add(new ArrayList<Integer>(temp));
}
else
{
temp.add(nums[pos]);
combine(nums, pos + 1, n);
temp.remove(temp.size() - 1);
combine(nums, pos + 1, n);
}
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
combine(nums, 0, nums.length);
return lists;
}
}