Subsets
Given a set of distinct integers,nums, return all possible subsets (the power set).
Note:The solution set must not contain duplicate subsets.
Example:
Analysis
Backtracking
典型的backtracking问题,可以用比较通用的模板来解决。
Iterative
While iterating through all numbers, for each new number, we can either pick it or not pick it
1, if pick, just add current number to every existing subset.
2, if not pick, just leave all existing subsets as they are.
We just combine both into our result.
For example, {1,2,3} intially we have an emtpy set as result [ [ ] ] Considering 1, if not use it, still [ ], if use 1, add it to [ ], so we have [1] now Combine them, now we have [ [ ], [1] ] as all possible subset
Next considering 2, if not use it, we still have [ [ ], [1] ], if use 2, just add 2 to each previous subset, we have [2], [1,2] Combine them, now we have [ [ ], [1], [2], [1,2] ]
Next considering 3, if not use it, we still have [ [ ], [1], [2], [1,2] ], if use 3, just add 3 to each previous subset, we have [ [3], [1,3], [2,3], [1,2,3] ] Combine them, now we have [ [ ], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3] ]
Bit Manipulation
Solution
Backtracking
Another Backtracking
Iteration
Bit Manipulation
Reference
Last updated