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], [] ]
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(num.length == 0){
return result;
}
Arrays.sort(num);
help(result,num,new ArrayList<Integer>(),0);
return result;
}
private void help(ArrayList<ArrayList<Integer>> result, int[] num, ArrayList<Integer> list, int start) {
result.add(new ArrayList<>(list));
for(int i = start ; i < num.length ; i++){
if( i != start && num[i] == num[i-1]){
continue;
}
list.add(num[i]);
help(result,num,list,i+1);
list.remove(list.size()-1);
}
}
}