首页 > 其他分享 >LeetCode 491 递增子序列

LeetCode 491 递增子序列

时间:2022-09-05 11:04:33浏览次数:56  
标签:vector set nums int 递增 491 num path LeetCode

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    int num = -101;
 

    void dfs(int start, vector<int>& nums) {
        if (path.size() > 1) {
            res.push_back(path);
        }

        unordered_set<int> set; //只负责这一层去重,不用回溯
        for (int i = start; i < nums.size(); i ++) {
            if (nums[i] >= num && set.find(nums[i]) == set.end()) { //没有找到重复元素
                set.insert(nums[i]);
                int temp = num;
                path.push_back(nums[i]);
                num = nums[i];
                dfs(i + 1, nums);
                path.pop_back();
                num = temp;
            }
        }
    }

    vector<vector<int>> findSubsequences(vector<int>& nums) {
        dfs(0, nums);



        return res;
    }
};

标签:vector,set,nums,int,递增,491,num,path,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16657327.html

相关文章