非递减子序列 (Non-decreasing Subsequences)

 

思路:

// @Title: 非递减子序列 (Non-decreasing Subsequences)
// @Author: qisiii
// @Date: 2024-09-20 00:40:49
// @Runtime: 6 ms
// @Memory: 48.8 MB
// @comment: 
// @flag: 
class Solution {
    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(result, new ArrayList<>(), nums, 0);
        return result;

    }

    private void dfs(List<List<Integer>> result, List<Integer> list, int[] nums, int start) {
        if (list.size() > 1) {
            result.add(new ArrayList<>(list));
        }
        if (start == nums.length) {
            return;
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = start; i < nums.length; i++) {
            if (list.size() > 0 &&
                    list.get(list.size() - 1) > nums[i]) {
                continue;
            }
            if (set.contains(nums[i])) {
                continue;
            } else {
                set.add(nums[i]);
            }
            list.add(nums[i]);
            dfs(result, list, nums, i + 1);
            list.remove(list.size() - 1);
        }
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18