https://oj.leetcode.com/problems/subsets-ii/
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
public List<List<Integer>> subsetsWithDup(int[] num)
哎,这题和Subset http://blog.youkuaiyun.com/chaochen1407/article/details/43369783 的区别就和 Combination Sum 跟 Combination Sum II 的区别是一样一样的。具体我就不再阐述了。大家就根据那两篇文章看吧。大致上Subset里面我也略有提到过。
public List<List<Integer>> subsetsWithDup(int[] num) {
List<List<Integer>> res = new LinkedList<List<Integer>>();
Integer[] buf = new Integer[num.length];
Arrays.sort(num);
helper(res, 0, 0, buf, num);
return res;
}
public void helper(List<List<Integer>> res, int curpos, int curlevel, Integer[] buf, int[] num){
Integer[] cur_res = new Integer[curpos];
System.arraycopy(buf, 0, cur_res, 0, curpos);
res.add(Arrays.asList(cur_res));
if(curlevel < num.length){
for(int i = curlevel; i < num.length; i++){
if(i != curlevel && num[i] == num[i - 1])//这个条件去掉了就是Subsets了。
continue;
buf[curpos] = num[i];
helper(res, curpos + 1, i + 1, buf, num);
}
}
}
1191

被折叠的 条评论
为什么被折叠?



