zoukankan      html  css  js  c++  java
  • 491. Increasing Subsequences

    Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .

    Example:

    Input: [4, 6, 7, 7]
    Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
    

    Note:

    1. The length of the given array will not exceed 15.
    2. The range of integer in the given array is [-100,100].
    3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

    用backtracking,set去重

    time: O(2^n), space: O(n)

    class Solution {
        public List<List<Integer>> findSubsequences(int[] nums) {
            Set<List<Integer>> res = new HashSet<>();
            if(nums == null || nums.length == 0) return new ArrayList<>(res);
            dfs(nums, 0, new ArrayList<>(), res);
            return new ArrayList<>(res);
        }
        private void dfs(int[] nums, int idx, List<Integer> tmp, Set<List<Integer>> res) {
            if(tmp.size() >= 2 && !res.contains(tmp))
                res.add(new ArrayList<>(tmp));
    
            for(int i = idx; i < nums.length; i++) {
                if(tmp.size() == 0 || nums[i] >= tmp.get(tmp.size() - 1)) {
                    tmp.add(nums[i]);
                    dfs(nums, i + 1, tmp, res);
                    tmp.remove(tmp.size() - 1);
                }
            }
        }
    }
  • 相关阅读:
    siteserver学习笔记
    移动端开发适配的2中方案
    移动端中适配问题
    2倍图3倍图怎么用
    常用的网站收藏
    关于用h5实现移动端的知识梳理
    悬浮广告代码
    vue中添加echarts
    VUE中给template组件加背景
    纯CSS控制背景图片100%自适应填充布局
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10078532.html
Copyright © 2011-2022 走看看