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 =
If S =
[1,2,2]
, a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
public class Solution { public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) { if(num==null) return null; Arrays.sort(num); ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); ArrayList<ArrayList<Integer>> prev = new ArrayList<ArrayList<Integer>>(); for(int i=num.length-1;i>=0;i--){ if(i==num.length-1||num[i]!=num[i+1]||prev.size()==0){ prev = new ArrayList<ArrayList<Integer>>(); for(int j=0;j<res.size();j++){ prev.add(new ArrayList<Integer>(res.get(j))); } } for(ArrayList<Integer> temp:prev){ temp.add(0,num[i]); } if(i==num.length-1||num[i]!=num[i+1]){ ArrayList<Integer> temp1 = new ArrayList<Integer>(); temp1.add(num[i]); prev.add(temp1); } for(ArrayList<Integer> temp:prev){ res.add(new ArrayList<Integer>(temp)); } } res.add(new ArrayList<Integer>()); return res; } }
No comments:
Post a Comment