Rewriting questions-Leetcode-78. Subset (backtracking, DFS)

78. Subset

Topic link

Source: LeetCode
Link: https://leetcode-cn.com/problems/subsets
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Title description

Give you an integer array nums, the elements in the array are different from each other. Return all possible subsets (power sets) of this array.

The solution set cannot contain duplicate subsets. You can return the solution set in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1, 2,3]]
Example 2:

Input: nums = [0]
Output: [[],[0]]

Topic analysis

1. Expansion method/violence method-one by one number expansion
Empty: []
Only 1: (add 1 to each subset) [1]
Add 2: (Add 2 to each subset) [2] [ 1,2]
plus 3: (add 3 to each subset) [3] [1, 3] [2, 3] [1, 2, 3]
No need to consider duplicate problems

class Solution {
    
    
public:
    vector<vector<int>> subsets(vector<int>& nums) {
    
    
        vector<vector<int>> res;
        res.push_back(vector<int>());
        for(int num : nums){
    
    
            vector<vector<int>> subset;//存放每个数字产生新加的子集扩展 例如num为3 此时res【【】【1】【2】【1,2】】 
            for(auto vec: res){
    
    
                vector<int> temp = vec;//temp为【】或【1】或【2】或【1,2】
                temp.push_back(num);
                subset.push_back(temp);
            }
            //此时subset为每个数产生的扩展 subset【【3】【1,3】【2,3】【1,2,3】】
            for(auto s:subset){
    
    
                res.push_back(s);
            }
        }
        return res;
    }
};

2. Backtracking recursion
length
0 【】
1 【1】【2】【3】
2 【1,2】【1,3】【2,3】
3 【1,2,3】
recursive downward each time The elements are the elements after the element

class Solution {
    
    
public:
    vector<vector<int>> res;
    vector<vector<int>> subsets(vector<int>& nums) {
    
    
        res.push_back(vector<int>());
        for(int length = 1; length <= nums.size(); length++){
    
    //length是长度 
            backtracking(nums, length, 0, vector<int>());
        }
        return res;
    }
    void backtracking(vector<int>& nums, int length, int index, vector<int> temp){
    
    
        if(temp.size() == length){
    
    //剪枝
            res.push_back(temp);
            return ;
        }
        for(int i = index; i < nums.size(); i++){
    
    
            temp.push_back(nums[i]);
            backtracking(nums, length, i+1,temp);
            temp.pop_back();
        }
    }
};

3.DFS depth-first algorithm-recursive (better to draw a picture) one way to the end

class Solution {
    
    
public:
    vector<vector<int>> res;//2 3
    vector<vector<int>> subsets(vector<int>& nums) {
    
    
        dfs(nums, 0, vector<int>());//找到子集temp加入res index开始的索引位置
        return res;
    }

    void dfs(vector<int>& nums, int index, vector<int> temp){
    
    
        res.push_back(temp);
        if(nums.size() == index){
    
    
            return ;
        }
        for(int i = index; i < nums.size(); i++){
    
    
            temp.push_back(nums[i]);
            dfs(nums, i + 1, temp);
            temp.pop_back();
        }
    }
};

Guess you like

Origin blog.csdn.net/qq_42771487/article/details/114389974