Title Link: subsets-ii
Import Java.util.arraylist;import java.util.arrays;import Java.util.hashset;import Java.util.list;import java.util.set;/** * Given A collection of integers that might contain duplicates, S, return all possible subsets. Note:elements in a subset must is 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], [up], []] * */public class Subsetsii {//19/ Test cases passed.//status:accepted//runtime:259 ms//submitted:0 minutes ago//time complexity O (2^n) space complexity O (n)//lazy, direct use of Set container , remove duplicate subset public set<list<integer>> subsets = new hashset<list<integer>> ();p ublic List<List <Integer>> subsetswithdup (int[] num) {arrays.sort (num); Subsets (NUM, 0, New arraylist<integer> ()); return new arraylist<list<integer>> (subsets); } public void subsets (int[] S, int step, list<integer> subset) {if (step = = s.length) {Subsets.add (subset); Return }//num[step] does not join the sub-set subsets (S, step + 1, new arraylist<integer> (subset)); Num[step] Join sub-centralized Subset.add (S[step]); Subsets (S, step + 1, new arraylist<integer> (subset)); } public static void Main (string[] args) {//TODO auto-generated method stub}}
[Leetcode 90] Subsets II