LeetCode[Backtracking]: Subsets

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

我的想法是常規解法,利用遞歸來解,我的C++代碼實現如下:

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int> > res;
        sort(S.begin(), S.end());
        subsets(S, -1, res);
        return res;
    }

private:
    void subsets(vector<int> &S, int start, vector<vector<int> > &res) {
        if (start != S.size()) {
            if (start == -1) res.push_back(vector<int> ());
            else
                for (int i = 0, size = res.size(); i < size; ++i) {
                    vector<int> set = res[i];
                    set.push_back(S[start]);
                    res.push_back(set);
                }
            subsets(S, start + 1 ,res);
        }
    }
};

這個discuss提出了一種令人驚喜的解法:https://leetcode.com/discuss/9213/my-solution-using-bit-manipulation

回覆裏面有非常好的解釋:

This is an amazing solution.Learnt a lot.Let me try to explain this to those who didn’t get the logic.
Number of subsets for {1 , 2 , 3 } = 2^3 .
why ?
case possible outcomes for the set of subsets
1 -> Take or dont take = 2
2 -> Take or dont take = 2
3 -> Take or dont take = 2
therefore , total = 2*2*2 = 2^3 = { { } , {1} , {2} , {3} , {1,2} , {1,3} , {2,3} , {1,2,3} }
Lets assign bits to each outcome -> First bit to 1 , Second bit to 2 and third bit to 3
Take = 1
Dont take = 0
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 }
In the above logic ,Insert S[i] only if (j>>i)&1 ==true { j E { 0,1,2,3,4,5,6,7 } i = ith element in the input array }
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
Time complexity : O(n*2^n) , for every input element loop traverses the whole solution set length i.e. 2^n

C++代碼實現如下:

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

這種方法的思維令人驚豔!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章