集合的所有子集

集合的所有子集

http://www.nowcoder.com/questionTerminal/c333d551eb6243e0b4d92e37a06fbfc9

回溯+sort函数:

//
// Created by jt on 2020/8/31.
//
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int> > res;
        dfs(res, S, 0, vector<int>());
        return res;
    }

    void dfs(vector<vector<int> > &res, vector<int> &S, int idx, vector<int> tmp) {
        if (idx > S.size() - 1) { sort(tmp.rbegin(), tmp.rend()); res.push_back(tmp); return; }
        dfs(res, S, idx+1, tmp);
        tmp.push_back(S[idx]);
        dfs(res, S, idx+1, tmp);
    }
};
刷遍天下无敌手 文章被收录于专栏

秋招刷题历程

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
机械打工仔:有说的你怀疑一下就行了,直接问也太实诚了
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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