代码随想录 Leetcode491. 非递减子序列

题目:

代码随想录 Leetcode491. 非递减子序列_第1张图片


代码(首刷看解析 2024年2月3日):

class Solution {
private:
    vector> res;
    vector path;
public:
    void backtracking(vector& nums, int startIndex) {
        if (path.size() > 1) {
            res.push_back(path);
        }

        unordered_set uset;
        for (int i = startIndex; i < nums.size(); ++i) {
            if (!path.empty() && nums[i] < path.back() ||
                uset.find(nums[i]) != uset.end()) {
                continue;
            }
            uset.insert(nums[i]);
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
        }
    }
    vector> findSubsequences(vector& nums) {
        backtracking(nums, 0);
        return  res;
    }
};

你可能感兴趣的:(#,leetcode,---medium,前端,算法,javascript)