LeetCode491. Non-decreasing Subsequences

文章目录

一、题目

Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

Example 1:

Input: nums = [4,6,7,7]

Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

Example 2:

Input: nums = [4,4,3,2,1]

Output: [[4,4]]

Constraints:

1 <= nums.length <= 15

-100 <= nums[i] <= 100

二、题解

既可以使用unordered_map进行判断是否在同一层上有重复元素,也可以使用unordered_set进行去重

cpp 复制代码
class Solution {
public:
    vector<int> path;
    vector<vector<int>> res;
    void backtracking(vector<int>& nums,int startIndex){
        if(path.size() > 1) res.push_back(path);
        unordered_map<int,int> map;
        for(int i = startIndex;i < nums.size();i++){
            if((!path.empty() && nums[i] < path.back()) || map[nums[i]] == 1) continue;
            map[nums[i]] = 1;
            path.push_back(nums[i]);
            backtracking(nums,i + 1);
            path.pop_back();
        }
    }
    vector<vector<int>> findSubsequences(vector<int>& nums) {
        backtracking(nums,0);
        return res;
    }
};
相关推荐
hillstream32 分钟前
从这次xAI重组说开去--用类比的思维来理解
人工智能·算法·xai·elon.mask
菜鸡儿齐5 分钟前
leetcode-最长连续序列
数据结构·算法·leetcode
寻寻觅觅☆6 分钟前
东华OJ-基础题-120-顺序的分数(C++)
开发语言·c++·算法
云小逸7 分钟前
【Vscode插件开发教程】VSCode插件开发入门指南:从C++开发者的视角
c++·ide·vscode
Tisfy9 分钟前
LeetCode 3713.最长的平衡子串 I:计数(模拟)
算法·leetcode·题解·模拟
月疯10 分钟前
陀螺仪和加速度计(模拟状态,计算运动状态)
算法
汉克老师13 分钟前
GESP2024年12月认证C++二级( 第二部分判断题(1-10))
c++·循环结构·分支结构·gesp二级·gesp2级
Σίσυφος190014 分钟前
双目立体视觉 数学推导(从 F → E → R,T)
算法
Hcoco_me24 分钟前
目标追踪概述、分类
人工智能·深度学习·算法·机器学习·分类·数据挖掘·自动驾驶
Ronin30528 分钟前
虚拟机数据管理模块
开发语言·c++·rabbitmq