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], [] ]包含重复元素的集合,求子集。同Subsets,只是添加进List时判断是否已经存在
public class Solution {
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
Arrays.sort(num);
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> tmp=new ArrayList<Integer>();
result.add(tmp);
for(int i=0;i<num.length;i++)
{
int len=result.size();
for(int j=0;j<len;j++)
{
tmp.clear();
tmp=(ArrayList<Integer>) result.get(j).clone();
tmp.add(num[i]);
if(!result.contains(tmp)) //判断是否已经存在
result.add((ArrayList<Integer>) tmp.clone());
}
}
return result;
}
}