• [leetcode-78-Subsets]


    Given a set of distinct integers, nums, return all possible subsets.
    Note: The solution set must not contain duplicate subsets.
    For example,
    If nums = [1,2,3], a solution is:
    [
    [3],
    [1],
    [2],
    [1,2,3],
    [1,3],
    [2,3],
    [1,2],
    []
    ]

    思路:

    首先排序,然后回溯法。

    void backtracking(vector<vector<int>>& subs,vector<int>& sub,vector<int>& nums,int start)
    {
      subs.push_back(sub);
      for(int i = start;i<nums.size();i++)
      {
        sub.push_back(nums[i]);
        backtracking(subs,sub,nums,i+1);
        sub.pop_back();
      }
    }
    vector<vector<int>> subsets(vector<int>& nums)
    {
      sort(nums.begin(),nums.end());
      vector<vector<int>> subs;
       vector<int>sub;
       backtracking(subs,sub,nums,0);   
      return subs;
    }

     另外,leetcode上大神有更详细多样的方法解析,如下:

    Recursive (Backtracking)

    This is a typical problem that can be tackled by backtracking. Since backtracking has a more-or-less similar template, so I do not give explanations for this method.

    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            vector<vector<int>> subs;
            vector<int> sub;  
            genSubsets(nums, 0, sub, subs);
            return subs; 
        }
        void genSubsets(vector<int>& nums, int start, vector<int>& sub, vector<vector<int>>& subs) {
            subs.push_back(sub);
            for (int i = start; i < nums.size(); i++) {
                sub.push_back(nums[i]);
                genSubsets(nums, i + 1, sub, subs);
                sub.pop_back();
            }
        }
    };
    

    Iterative

    This problem can also be solved iteratively. Take [1, 2, 3] in the problem statement as an example. The process of generating all the subsets is like:

    1. Initially: [[]]
    2. Adding the first number to all the existed subsets: [[], [1]];
    3. Adding the second number to all the existed subsets: [[], [1], [2], [1, 2]];
    4. Adding the third number to all the existed subsets: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]].

    Have you got the idea :-)

    The code is as follows.

    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            vector<vector<int>> subs(1, vector<int>());
            for (int i = 0; i < nums.size(); i++) {
                int n = subs.size();
                for (int j = 0; j < n; j++) {
                    subs.push_back(subs[j]); 
                    subs.back().push_back(nums[i]);
                }
            }
            return subs;
        }
    }; 
    

    Bit Manipulation

    This is the most clever solution that I have seen. The idea is that to give all the possible subsets, we just need to exhaust all the possible combinations of the numbers. And each number has only two possibilities: either in or not in a subset. And this can be represented using a bit.

    There is also another a way to visualize this idea. That is, if we use the above example, 1 appears once in every two consecutive subsets, 2 appears twice in every four consecutive subsets, and 3 appears four times in every eight subsets, shown in the following (initially the 8 subsets are all empty):

    [], [], [], [], [], [], [], []

    [], [1], [], [1], [], [1], [], [1]

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

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

    The code is as follows.

    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            int num_subset = pow(2, nums.size()); 
            vector<vector<int> > res(num_subset, vector<int>());
            for (int i = 0; i < nums.size(); i++)
                for (int j = 0; j < num_subset; j++)
                    if ((j >> i) & 1)
                        res[j].push_back(nums[i]);
            return res;  
        }
    };
    

    Well, just a final remark. For Python programmers, this may be an easy task in practice since the itertools package has a function combinations for it :-)

    参考:

    https://discuss.leetcode.com/topic/19110/c-recursive-iterative-bit-manipulation-solutions-with-explanations

  • 相关阅读:
    机器学习漫游(1) 基本设定
    win10+anaconda+cuda配置dlib,使用GPU对dlib的深度学习算法进行加速(以人脸检测为例)
    基于SignalR的站点有连接数限制问题及解决方案
    如何删除EF4.0以上的版本
    从JDBC到hibernate再到mybatis之路
    ORA-01747: user.table.column, table.column 或列说明无效 异常解决方法总结
    MyBatis Oracle 批量新增、更新和删除
    IDEA 方法注释模板和类注释模板
    MyBatis 使用注解方式实现一对多
    (转)GPT磁盘与MBR磁盘区别
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/6938565.html
Copyright © 2020-2023  润新知