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;
    }
};
相关推荐
_深海凉_5 小时前
LeetCode热题100-翻转二叉树
算法·leetcode·职场和发展
许长安5 小时前
gRPC Keepalive 机制
c++·经验分享·笔记·rpc
吃好睡好便好5 小时前
在Matlab中绘制抛物三维曲面图
开发语言·人工智能·学习·算法·matlab·信息可视化
tyung5 小时前
用 Go 实现一个生产级 Ring Buffer Queue:环形数组、位运算取模、批量操作全拆解
数据结构·go
伯远医学5 小时前
Nat. Methods | 邻近标记技术:活细胞中捕捉分子互作的新利器
java·开发语言·前端·javascript·人工智能·算法·eclipse
wangjialelele5 小时前
Linux SystemV 消息队列 + 责任链模式:实现客户端消息处理流水线
linux·服务器·c语言·网络·c++·责任链模式
刘永鑫Adam5 小时前
Nature Microbiology | 基于TRACS算法的跨多界宏基因组数据菌株水平溯源推演
算法
小O的算法实验室5 小时前
2026年SEVC,面向无人机辅助边缘计算的自适应群体智能算法,深度解析+性能实测
算法·边缘计算·智能算法·智能算法改进
高锰酸钾_5 小时前
计算机网络-网络层-路由算法与路由协议
计算机网络·算法·智能路由器
智者知已应修善业6 小时前
51单片机4按键控制共阳LED霓虹灯切换1整体闪烁2流水下3流水上4间隔闪烁】2023-10-27
c++·经验分享·笔记·算法·51单片机