My simple dfs solution


  • 1
    N

    using condition temp.back() < source[i] ,guarantee non-descending order.

    class Solution {
    public:
        vector<vector<int> > res;
        vector<int> source;
        vector<vector<int> > subsets(vector<int> &S) {
            res.clear();
            source = S;
            vector<int> t;
            dfs(t);
            return res;
        }
        void dfs(vector<int> temp){
            res.push_back(temp);
            for(int i=0;i<source.size();i++){
                if(temp.empty()||temp.back() < source[i]){
                    temp.push_back(source[i]);
                    dfs(temp);
                    temp.pop_back();
                }
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.