Leetcode 90 subsets 2, Lintcode
Q: Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: Elements in a subset must be in non-descending order; The solution set must not contain duplicate subsets.
java version 1
import java.util.Arrays;public class Solution { public List
> subsetsWithDup(int[] nums) { List
> ans = new ArrayList
>(); if (nums == null) return ans; List ele = new ArrayList (); Arrays.sort(nums); addElement(ans, ele, nums, 0, nums.length-1); return ans; } public void addElement(List
> ans, List ele, int[] nums, int start, int end ){ if (start > end){ ans.add(new ArrayList (ele)); return; } for(int i = start; i <= end; i++){ if (i > start && nums[i] == nums[i-1]){ continue; }else{ ele.add(nums[i]); addElement(ans, ele, nums, i+1, end); ele.remove(ele.size()-1); } } ans.add(new ArrayList (ele)); return; }}