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
没有技巧, 硬做就好
use std::collections::HashSet;
impl Solution {
pub fn find_subsequences(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut set = HashSet::new();
let mut ans: Vec<Vec<i32>> = Vec::new();
for i in 0..nums.len() {
for j in 0..ans.len() {
// 如果当前数字大于任意递增数组的最后一个数字, 则复制该递增数组并将当前数字push进copy,并将copy放到ans中
if nums[i] >= ans[j][ans[j].len() - 1] {
let mut l = ans[j].clone();
l.push(nums[i]);
if set.contains(&l) {
continue;
}
set.insert(l.clone());
ans.push(l);
}
}
ans.push(vec![nums[i]])
}
// 过滤出长度>1的递增数组
ans.into_iter().filter(|l| l.len() > 1).collect()
}
}