C++ easy solution


  • 0
    C

    /*
    Note: Use set instead of unordered_set, otherwise, you'll have to come up with a hash though, since the default one (std::hash<std::vector<int>>) will not be implemented.
    http://stackoverflow.com/questions/29855908/c-unordered-set-of-vectors

    */
    class Solution {
    public:
    vector<vector<int>> findSubsequences(vector<int>& nums) {
    vector<vector<int>> res;
    vector<int> path;
    set<vector<int>> uset;

        dfs(uset, path, nums, 0);
        
        for (auto u: uset){
            res.push_back(u);
        }
        
        return res;
    }
    
    void dfs(set<vector<int>>& res, vector<int>& path, vector<int>& nums, int idx){
        if (path.size()>1){
            res.insert(path);
        }
        
        for (int i=idx; i<nums.size(); i++){
            if (idx>0&&nums[i]<nums[idx-1])
                continue;
            
            path.push_back(nums[i]);
            dfs(res, path, nums, i+1);
            path.resize(path.size()-1);
        }
    }
    

    };


Log in to reply
 

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