[LeetCode] Subsets(!!!!!回溯&&迭代&&位操作)(to be updated)

Subsets Given a set of distinct integers, nums, 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 nums
= [1,2,3], a solution is:

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

前面那题combinations的应用而已(回溯法)

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> vv;
        for(int i = 0; i<=nums.size(); ++i)
            combine(vv, nums, i) ;
        return vv;

    }

    void combine(vector<vector<int>> &vv, vector<int>& n, int k) {
        vector<int> curr;
        int i =0;
        combinations(n,k,i,vv,curr);
    }

    void combinations(vector<int>& n, int k, int i, vector<vector<int>> &vv, vector<int> &curr){
        if(curr.size()==k){
            vv.push_back(curr);
            return;
        }
        for(; i<n.size(); ++i){
                curr.push_back(n[i]);
                combinations(n,k,i+1,vv,curr);
                curr.pop_back();
        }
    }
};

8ms Ac又不是最快的 o(╯□╰)o

[LeetCode]Permutations Iterative这道题中,我们的第三种方法,采用插入的方式一步步扩充这个集合。这样在这道求组合数的题目中同样可以使用,

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:

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

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;
    }
};

第三种逆天的方法 - - !
先转换成高中的排列组合问题,“3个不同的数,可以从中选0,1,2,3个数,可以组成多少种组合”
其实对于每一种组合来说,每一个数都有唯一的状态,选或是不选,这样共有2*2*2=2^3=8中组合。

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的数是001,011,101,111,就是奇数(第1位为1)
插入2的数是010,011,110,111,就是第二位为1的数
对三同理

element 1 is inserted only into those places where 1st bit of j is 1
if( j >> 0 &1 ) ==> for above above eg. this is true for sl.no.( j )=
1 , 3 , 5 , 7

element 2 is inserted only into those places where 2nd bit of j is 1
if( j >> 1 &1 ) == for above above eg. this is true for sl.no.( j ) =
2 , 3 , 6 , 7

element 3 is inserted only into those places where 3rd bit of j is 1
if( j >> 2 & 1 ) == for above above eg. this is true for sl.no.( j )
= 4 , 5 , 6 , 7

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; 
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值