https://leetcode.com/problems/non-decreasing-subsequences/description/
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]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Solution { public List<List<Integer>> findSubsequences(int[] nums) { Set<List<Integer>> res = new HashSet<>(); helper(nums, 0, new ArrayList<>(), res); return new ArrayList<>(res); } private void helper(int[] nums, int index, List<Integer> curr, Set<List<Integer>> res){ if(index == nums.length){ if(curr.size()>1){ res.add(new ArrayList<>(curr)); } return; } if(curr.isEmpty() || nums[index] >= curr.get(curr.size()-1)){ curr.add(nums[index]); helper(nums, index+1, curr, res); curr.remove(curr.size()-1); } helper(nums, index+1, curr, res); } } |