集合的所有子集
集合的所有子集
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);
}
};刷遍天下无敌手 文章被收录于专栏
秋招刷题历程

