为什么80%的码农都做不了架构师?>>>   

问题:

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.

解决:

① 递归解决。

class Solution {//62ms
    public List<List<Integer>> findSubsequences(int[] nums) {
        Set<List<Integer>> set = new HashSet<>();//避免重复
        List<Integer> list = new ArrayList<>();
        dfs(nums,0,list,set);
        return new ArrayList<>(set);
    }
    public void dfs(int[] nums,int i,List<Integer> list,Set<List<Integer>> set){
        if (list.size() >= 2){
            set.add(new ArrayList<>(list));
        }
        for (int j = i;j < nums.length;j ++){
            if (list.size() == 0 || list.get(list.size() - 1) <= nums[j]){
                list.add(nums[j]);
                dfs(nums,j + 1,list,set);
                list.remove(list.size() - 1);
            }
        }
    }
}

② 使用额外的标记记录重复。

class Solution {//25ms
    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(nums,0,list,res);
        return res;
    }
    public void dfs(int[] nums,int i,List<Integer> list,List<List<Integer>> res){
        if (list.size() >= 2){
            res.add(new ArrayList<>(list));
        }
        if (i == nums.length) return;
        Set<Integer> isused = new HashSet<>(); 
        for (int j = i;j < nums.length;j ++){
            if (isused.contains(nums[j])) continue;
            if (list.isEmpty() || nums[j] >= list.get(list.size() - 1)){
                isused.add(nums[j]);
                list.add(nums[j]);
                dfs(nums,j + 1,list,res);
                list.remove(list.size() - 1);
            }
        }
    }
}

转载于:https://my.oschina.net/liyurong/blog/1603734

递增子序列 Increasing Subsequences相关推荐

  1. 491. Increasing Subsequences 递增子序列

    给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2. 示例: 输入: [4, 6, 7, 7] 输出: [[4, 6], [4, 7], [4, 6, 7], [4, ...

  2. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  3. C++longest increasing subsequence 最长递增子序列的实现之二(附完整源码)

    C++longest increasing subsequence 最长递增子序列的实现 C++longest increasing subsequence 最长递增子序列的的实现完整源码(定义,实现 ...

  4. C++longest increasing subsequence 最长递增子序列的实现之一(附完整源码)

    C++longest increasing subsequence 最长递增子序列的实现 C++longest increasing subsequence 最长递增子序列的的实现完整源码(定义,实现 ...

  5. leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)

    参考 Python 解法: 动态规划 -- 最长递增子序列(LIS) 原题位置:Longest Increasing Subsequence | LeetCode OJ 题目的说明: 严格递增: 子序 ...

  6. [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. 最长递增子序列 子串_最长递增子序列

    最长递增子序列 子串 Description: 描述: This is one of the most popular dynamic programming problems often used ...

  8. 动态规划(最长递增子序列)---最长递增子序列

    最长递增子序列 300. Longest Increasing Subsequence (Medium) 题目描述:   给定一个数组,找到它的最长递增子序列 思路分析:   动态规划思想,定义一个数 ...

  9. 最长递增子序列问题合集

    最长递增子序列问题 合并LIS(JLIS) 第k个最大递增子序列(KLIS) 对于求单个序列的最长递增子序列网上的太多就不多写了,以下是它的一些进阶算法 合并LIS(JLIS) 题意 两个整数序列A和 ...

最新文章

  1. 工作10年后,再看String s = new String(xyz) 创建了几个对象?
  2. 独家 | 使用机器学习加速对非结构化数据的查询-第1部分(使用BlazeIt加速聚合和限制查询)...
  3. .net WCF简单实例
  4. 外汇汇率接口 java_基于JAVA的货币汇率api调用代码实例
  5. Py之cython:python库之cython的简介、安装、使用方法之详细攻略
  6. 【学习笔记】15、标准数据类型—集合
  7. CSLA.Net 3.0.5 项目管理示例 业务集合基类(ProjectResources.cs,ProjectResource.cs)
  8. android自定义布局实现优惠券效果
  9. HTML5中的webSocket、ajax、http
  10. rust风化速度_反驳《Golang、Rust的执行速度的对照,让人大吃一惊。》——不会别瞎说...
  11. thinkPHP 阿里云OSS 上传文件、直接下载
  12. 一种数据结构的封装模式
  13. type=xhr的500错误
  14. 50行代码实现的一个最简单的基于 DirectShow 的视频播放器
  15. CRM中复制记录的方法
  16. Ubuntu 16上命令行提示长目录的解决办法
  17. arduino与风向传感器的接线_Arduino动手做(8)湿度传感器模块
  18. VS2017 CUDA编程学习实例2:CUDA实现秩排序
  19. 100以内的三连加减法JAVA_100以内的加减法巧算(转)
  20. 切割视频——将视频截取python实现

热门文章

  1. 人生时间计算器_卡西欧计算器见证你与时间赛跑,计算人生,把握人生!
  2. c++遍历文件夹下的文件_算法面试|开发者必备|使用递归函数进行无限分类及文件夹遍历...
  3. 浮栅场效应管 符号_MOS场效应管
  4. 2018湖北技能高考计算机类学校,武汉船舶职业技术学院举行2018年湖北省技能高考...
  5. oracle根据当前月份往前,oracle获取系统日期--当前时间+前一天+当前月+前一个月...
  6. 含有swap的c语言冒泡排序6,c#中写个Swap方法来实现冒泡排序 看看哪里错了
  7. Linux管道的原子性,管道的原子性 linux写操作原子性
  8. mysql分表全局查询_mysql如何查询多样同样的表/sql分表查询、java项目日志表分表的开发思路/按月分表...
  9. python修改zip文件内容_windows-将zip文件内容提取到Python 2.7中的特定目录
  10. Spring AOP注解