c++ dfs solution


  • 0
    vector<vector<int>> findSubsequences(vector<int>& nums) 
    {
        set<vector<int> > result;
        vector<int> subseq;
        dfs(nums, 0, subseq, result);
        
        return vector<vector<int> >(result.begin(), result.end());
    }
    
    void dfs(vector<int>& nums, int index, vector<int>& subseq, set<vector<int> >& result)
    {
        if (subseq.size() >= 2)
            result.insert(subseq);
        for (int i = index; i < nums.size(); i++)
        {
            if (subseq.empty() || (nums[i] >= subseq.back()))
            {
                subseq.push_back(nums[i]);
                dfs(nums, i + 1, subseq, result);
                subseq.pop_back();
            }
        }
    }

Log in to reply
 

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