LeetCode Subsets

 1 class Solution {
 2 public:
 3     vector<vector<int> > subsets(vector<int> &S) {
 4         sort(S.begin(), S.end());
 5         vector<vector<int> > res;
 6         vector<int> path;
 7         dfs(S, path, 0, res);
 8         return res;
 9     }
10     
11     void dfs(vector<int>& s, vector<int>& cur, int pos, vector<vector<int> >& res) {
12         if (pos == s.size()) {
13             res.push_back(cur);
14             return;
15         }
16         // case 1. put the current number into the set
17         cur.push_back(s[pos]);
18         dfs(s, cur, pos + 1, res);
19         cur.pop_back();
20         
21         // case 2. skip the current number
22         dfs(s, cur, pos + 1, res);
23     }
24 };

起来水一发

第二轮:

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],
  []
]

写一发Java版本:

public class Solution {
    private List<List<Integer>> res = new LinkedList<List<Integer>>();
    public List<List<Integer>> subsets(int[] S) {
        res.clear();
        
        Arrays.sort(S);
        List<Integer> current = new LinkedList<Integer>();
        dfs(S, 0, current);
        return res;
    }
    
    private void dfs(int[] S, int pos, List<Integer> current) {
        if (pos == S.length) {
            res.add(new ArrayList<Integer>(current));
            return;
        }
        
        current.add(S[pos]);
        dfs(S, pos + 1, current);
        current.remove(current.size() - 1);
    
        dfs(S, pos + 1, current);
    }
}

 找到另外一种递归解法,不过时间反而长了:

public class Solution {
    private List<List<Integer>> res = new LinkedList<List<Integer>>();
    
    public List<List<Integer>> subsets(int[] nums) {
        res.clear();
        
        Arrays.sort(nums);

        dfs(nums, 0, new ArrayList<Integer>());
        
        return res;
    }
    
    private void dfs(int[] nums, int pos, List<Integer> current) {
        int len = nums.length;
        if (pos > len) {
            return;
        }
        
        res.add(new ArrayList<Integer>(current));
        
        for (int i = pos; i<len; i++) {
            current.add(nums[i]);
            dfs(nums, i + 1, current);
            current.remove(current.size() - 1);
        }
        
    }
}

非递归解法:

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        
        sort(nums.begin(), nums.end());
        
        int len = nums.size();
        int total = 1<<len;
        vector<vector<int>> res(total);
        
        int last_size = 1;
        for (int i=0; i<len; i++) {
            for (int j=0; j<last_size; j++) {
                res[last_size + j] = res[j];
                res[last_size + j].push_back(nums[i]);
            }
            last_size = last_size * 2;
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/lailailai/p/3660000.html