题解 | #集合的所有子集(一)#

集合的所有子集(一)

https://www.nowcoder.com/practice/c333d551eb6243e0b4d92e37a06fbfc9


#include <vector>
class Solution {    
public:
void dfs(vector<vector<int>> & ans, vector<int> temp, vector<int> S, int start,int l){
    if(l == temp.size()) {
        ans.push_back(temp);
        return;
    }

    for(int i = start; i < S.size(); i ++){
        temp.push_back(S[i]);
        dfs(ans, temp, S, i + 1, l);
        temp.pop_back();
    }
    return;
}

    vector<vector<int> > subsets(vector<int>& S) {
        vector<vector<int>> ans;
        vector<int> temp;
        ans.push_back(temp);
        for(int i = 1; i <= S.size(); i ++ ){
            dfs(ans, temp, S, 0, i);//这个代码相当于是每一层一个for,第几层就会有几个元素
        }
        return ans;
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务