Leetcode: Subsets

Given a set of distinct integers, S, return all possible subsets.

Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Adopted approach: Notice that in line 11, you should create a copy of current path, and add it to res. Otherwise, it'll be edited later, and be wiped out to []. So the res will be [[], [], [], [], [], ...]

 1 class Solution {
 2     public List<List<Integer>> subsets(int[] nums) {
 3         List<List<Integer>> res = new ArrayList<>();
 4         List<Integer> path = new ArrayList<>();
 5         backtracking(path, res, nums, 0);
 6         return res;
 7     }
 8     
 9     public void backtracking(List<Integer> path, List<List<Integer>> res, int[] nums, int pos) {
10         if (pos == nums.length) {
11             res.add(new ArrayList<Integer>(path));
12             return;
13         }
14         // not to add current element to the subset
15         backtracking(path, res, nums, pos + 1);
16         
17         // add current element to the subset
18         path.add(nums[pos]);
19         backtracking(path, res, nums, pos + 1);
20         path.remove(path.size() - 1);
21     }
22 }

第二遍做法:

 1 public List<List<Integer>> subsets(int[] nums) {
 2     List<List<Integer>> list = new ArrayList<>();
 3     Arrays.sort(nums);
 4     backtrack(list, new ArrayList<>(), nums, 0);
 5     return list;
 6 }
 7 
 8 private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){
 9     list.add(new ArrayList<>(tempList));
10     for(int i = start; i < nums.length; i++){
11         tempList.add(nums[i]);
12         backtrack(list, tempList, nums, i + 1);
13         tempList.remove(tempList.size() - 1);
14     }
15 }

网上看到Bit Manipulation做法:

Take = 1
Dont take = 0
 
0) 0 0 0  -> Dont take 3 , Dont take 2 , Dont take 1 = { } 
1) 0 0 1  -> Dont take 3 , Dont take 2 ,   take 1       =  {1 } 
2) 0 1 0  -> Dont take 3 ,    take 2       , Dont take 1 = { 2 } 
3) 0 1 1  -> Dont take 3 ,    take 2       ,      take 1    = { 1 , 2 } 
4) 1 0 0  ->    take 3      , Dont take 2  , Dont take 1 = { 3 } 
5) 1 0 1  ->    take 3      , Dont take 2  ,     take 1     = { 1 , 3 } 
6) 1 1 0  ->    take 3      ,    take 2       , Dont take 1 = { 2 , 3 } 
7) 1 1 1  ->    take 3     ,      take 2     ,      take 1     = { 1 , 2 , 3 } 
 1 public class Solution {
 2     public List<List<Integer>> subsets(int[] nums) {
 3         List<List<Integer>> res = new ArrayList<>();
 4         if (nums==null || nums.length==0) return res;
 5         Arrays.sort(nums);
 6         int totalNum = 1<<nums.length;
 7         for (int i=0; i<totalNum; i++) { //one i correspond to one subset pattern
 8             List<Integer> path = new ArrayList<>();
 9             for (int k=0; k<nums.length; k++) {
10                 if (((i>>k)&1) == 1) 
11                     path.add(nums[k]);
12             }
13             res.add(new ArrayList<Integer>(path));
14         }
15         return res;
16     }
17 }
 
原文地址:https://www.cnblogs.com/EdwardLiu/p/3740980.html