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 <= numsi <= 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;
    }
};
相关推荐
Chen_harmony2 分钟前
六、排序详解
c语言·数据结构·算法·排序算法
ShineWinsu7 分钟前
对于Redis:List类型的解析
数据库·c++·redis·链表·缓存·面试·list
-dzk-14 分钟前
【贪心算法】LC 55.跳跃游戏
算法·游戏·贪心算法
ofoxcoding17 分钟前
Jev 模型评测深度解析:核心得分、能力局限与最佳适用任务场景
人工智能·算法·机器学习·ai
Evand J33 分钟前
【MATLAB例程】三维A*路径规划与TOA-AOA-TDOA融合定位算法。附下载链接
数据库·算法·matlab
Doubbbbbbble云42 分钟前
图算法中的剪枝技术与启发式优化分析4
算法·机器学习·剪枝
鶴哥只手遮天1 小时前
CFL3D深度解析:NASA经典CFD求解器的架构、算法与工程实践
算法
维克兜率天1 小时前
【维克】价差交易实战:配对交易的核心参数设置
大数据·笔记·算法·量化
dtq04241 小时前
数据结构 - 树的概念
c语言·数据结构·学习·算法
晒太羊的猫1 小时前
hot100多维动态规划刷题
算法·动态规划