回溯

class Solution {
public:
    vector<vector<int>> res;
    vector<int> vec;
    void helper(const vector<int>& S, int start, int n){
        if(start == n) return;
        for(int i = start; i < n; i++){
            vec.push_back(S[i]);
            res.push_back(vec); //每个节点都是一个子集
            helper(S, i + 1, n);
            vec.pop_back();
        }
    }
    vector<vector<int> > subsets(vector<int> &S) {
        res.push_back({});
        helper(S, 0, S.size());
        sort(res.begin(), res.end(), [](vector<int> a, vector<int> b){
            if(a.size() == b.size()) return a < b;
            else return a.size() < b.size();
        });
        return res;
    }
};