Leecode 0491. Non-decreasing Subsequences
491. 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:
1 | Input: nums = [4,6,7,7] |
Example 2:
1 | Input: nums = [4,4,3,2,1] |
题目大意
给定一个整数数组 nums
,返回所有不同的、长度至少为 2 的非递减子序列。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。
例如:
- 输入
nums = [4,6,7,7]
,输出包含[4,6]
、[4,6,7]
等多个符合条件的非递减子序列; - 输入
nums = [4,4,3,2,1]
,输出只有[[4,4]]
(唯一符合条件的子序列)。
解题思路
核心思路是递归回溯 + 去重,通过枚举所有可能的子序列,筛选出非递减且长度≥2 的子序列,并确保结果无重复:
- 递归回溯:
- 从数组的每个位置开始,尝试构建子序列;
- 对于每个位置
i
,若当前元素大于等于子序列的最后一个元素(保持非递减),则将其加入子序列; - 递归处理下一个位置,探索更长的子序列;
- 回溯时移除最后加入的元素,尝试其他可能的选择。
- 去重策略:
- 在同一层递归中,若遇到与之前处理过的元素相同的元素,跳过该元素(避免生成重复子序列);
- 使用临时集合记录当前层已处理的元素,确保每个元素只被考虑一次。
- 终止条件:
- 当子序列长度≥2 时,将其加入结果列表;
- 当遍历完数组所有元素时,终止递归。
代码实现
1 | #include <vector> |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.