【Lintcode】017.Subsets

本文介绍了一种生成集合所有可能子集的算法,并提供了三种不同的实现方法:迭代法、递归法及位操作法。通过这些方法,可以有效地解决组合问题中的子集生成任务。

题目:

题解:

Solution 1 ()

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

Solution 1.2

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

    return res;
    }
};

Solution 2 ()

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int> > res;
        vector<int> v;
        sort(S.begin(), S.end());
        dfs(res, S, v, 0);
        return res;
    }
    void dfs(vector<vector<int> > &res, vector<int> S, vector<int> &v, int pos) {
        res.push_back(v);
        for (int i = pos; i < S.size(); ++i) {
            v.push_back(S[i]);
            dfs(res, S, v, i + 1);
            v.pop_back();
        }
    }
};

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]

Solution 3 ()

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& S) {
        sort(S.begin(), S.end());
        int num_subset = pow(2, S.size()); 
        vector<vector<int> > res(num_subset, vector<int>());

        for (int i = 0; i < S.size(); i++) {
            for (int j = 0; j < num_subset; j++) {
                if ((j >> i) & 1) {
                    res[j].push_back(S[i]);
                }
            }
        }
        return res;  
    }
};

 

转载于:https://www.cnblogs.com/Atanisi/p/6869189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值