Given a set of distinct integers, nums, 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 nums = [1,2,3]
, a solution is:
[ 3], [1], [2], [1,3], [+] , [2,3], [up ], []]
Algorithm idea: for example {}{1}{2}{1,2} all subsets of {* *} may be assumed to be S1, after adding 3 that is {The full subset of the {? The simple way to do this is to add 3 to the subset of {3}{1,3}{2,3}{1,2,3} to get the new subset {*.} to S2,
S1∩S2 is a full subset of {.
Public classSolution { Publiclist<list<integer>> Subsets (int[] nums) {List<List<Integer>> re =NewArraylist<list<integer>>(); Re.add (NewArraylist<integer> ());//Initialize, add an empty setArrays.sort (nums); for(inti:nums) {List<List<Integer>> tmp =NewArraylist<list<integer>>(); for(list<integer>set:re) {List<Integer> Tmp_set =NewArraylist<integer>(); Tmp_set.addall (set);//Clone original Existing listTmp_set.add (i); Tmp.add (Tmp_set); } re.addall (TMP); } returnre; }}
Subsets *