c++ dfs


  • 0
    J

    '''

    void dfs(vector<vector<int>> &res, vector<int> &cur, vector<int> &nums, int idx)
    {
        if (cur.size() > 1)
        {
            res.push_back(cur);
        }
        
        unordered_set<int> went;
        for (int i = idx; i < nums.size(); i++)
        {
            if (!went.count(nums[i]))
            {
                if (cur.size() == 0 || nums[i] >= cur.back())
                {
                    cur.push_back(nums[i]);
                    went.insert(nums[i]);
                    dfs(res, cur, nums, i + 1);
                    cur.pop_back();
                }
            }
        }
    }
    
    vector<vector<int>> findSubsequences(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> cur;
        dfs(res, cur, nums, 0);
        return res;
    }
    

    '''


Log in to reply
 

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