标签:
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
For example,
If S = [1,2,2]
,
a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]题意:输出组合,不能重复。
思路:再普通组合的情况下加上一个条件就是如果排序后数字重复的话,我们就只取第一个。
public class Solution { public List<List<Integer> > ans = new ArrayList<List<Integer> >(); void dfs(int[] num, List<Integer> tmp, int cur) { List<Integer> t = new ArrayList<Integer>(tmp); ans.add(t); for (int i = cur; i < num.length; i++) { if (i != cur && num[i] == num[i-1]) continue; tmp.add(num[i]); dfs(num, tmp, i+1); tmp.remove(tmp.size()-1); } } public List<List<Integer> > subsetsWithDup(int[] num) { Arrays.sort(num); ans.clear(); List<Integer> tmp = new ArrayList<Integer>(); dfs(num, tmp, 0); for (List<Integer> i : ans) System.out.println(i.toString()); return ans; } }
标签:
原文地址:http://blog.csdn.net/u011345136/article/details/45049659