title : Matrix 0
Difficulty : Easy
topic content :
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note:the solution set must not contain duplicate subsets.
translation :
Given a different set of integers, nums, returns all possible subsets (both the Empty and self).
Note: The solution set cannot contain duplicate subsets.
Example:
Input:nums = [1,2,3]output:[ [3], [1], [2], [], [1,3], [2,3], [up], []]
my train of thought : none .....
Answer code :
1 PublicList<list<integer>> Subsetswithdup (int[] nums) {2List<list<integer>> List =NewArraylist<>();3 Arrays.sort (nums);4Backtrack (list,NewArraylist<> (), Nums, 0);5 returnlist;6 }7 8 Private voidBacktrack (list<list<integer>> List, list<integer> templist,int[] Nums,intstart) {9List.add (NewArraylist<>(templist));Ten for(inti = start; i < nums.length; i++){ One Templist.add (Nums[i]); ABacktrack (list, templist, nums, i + 1); -Templist.remove (Templist.size ()-1); - } the}
complexity of the answer : O (n2)
Answer Ideas :
In fact, using the idea of backtracking recursion, we write a backtracking method, which is a subset of the subsets of each of the subsets, which are composed of the beginning of each letter. ) Such a collection,
Therefore, the method only needs to add templist to the result set list, and then write a loop, starting from start, the current nums[i] is added to the templist, and then recursively call the backtracking method (Start= i+1), after the call is complete with nums[i] The beginning of the subset ends, so nums[i] is removed from the templist.
Here's the process: Take the input "templist" as an example (you can see it in the first line of the backtracking method)
[] ——————— start from empty
[1] —————— first with a null opening
[1, 2] —————— the first to begin with 1
[1, 2, 3] —————— the first with the beginning of 12, at this time to 3, stating the end of a subset beginning with 12 , deleting 2, at which point back to beginning with 1
[1, 3] —————— the second beginning with 1, and then to 3, ending with a subset beginning with 1 , deleting 1, and backtracking to the opening of an empty
[2] —————— the second with a null opening
[2, 3] —————— the first starting with 2, at this point to 3, ending with a subset beginning with 2 , deleting 2, backtracking to an empty opening
[3] —————— the last one with an empty opening
extension : What should I do when nums contains repeating characters ?
1, the beginning is disorderly order, so need to nums sorting to determine whether to repeat.
2. In the loop of backtracking method, the first line is added to repeat judgment, if the current element (not the first) and the previous element, then skip this element does not use recursion.
1 PublicList<list<integer>> Subsetswithdup (int[] nums) {2List<list<integer>> List =NewArraylist<>();3 Arrays.sort (nums);4Backtrack (list,NewArraylist<> (), Nums, 0);5 returnlist;6 }7 8 Private voidBacktrack (list<list<integer>> List, list<integer> templist,int[] Nums,intstart) {9List.add (NewArraylist<>(templist));Ten for(inti = start; i < nums.length; i++){ One if(i > Start && nums[i] = = Nums[i-1])Continue;//Skip Duplicates A Templist.add (Nums[i]); -Backtrack (list, templist, nums, i + 1); -Templist.remove (Templist.size ()-1); the } -}
Leetcode [78] (Java): subsets (for subset) extension--[90] Title: Subsets 2