Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
1) Elements in a subset must be in non-descending order.
2) The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
1 2 3 4 5 6 7 8 9 10 |
[ [2], [1], [1,2,2], [2,2], [1,2], [] ] |
Solution code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> mset = new ArrayList<List<Integer>>(); mset.add(new ArrayList<Integer>()); Arrays.sort(nums); for (int i = 0; i < nums.length; i++) { int size = mset.size(); for (int m = 0; m < size; m++) { List<Integer> tmp = mset.get(m); List<Integer> tmp2 = new ArrayList<Integer>(tmp); tmp2.add(nums[i]); if (!mset.contains(tmp2)) { mset.add(tmp2); } } } return mset; } } |