原题链接在这里:https://leetcode.com/problems/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:

  1. Input: [4, 6, 7, 7]
  2. 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.

题解:

The DFS needs the state, current start index, current item.

When item size is already >= 2, then add a copy to res.

Otherwise, iterate from start, if nums[i] >= last number in item, then add it to item.

When backtracking, remove the last added number.

Time Complexity: exponential.

Space: O(n). n = nums.length.

AC Java:

  1. class Solution {
  2. public List<List<Integer>> findSubsequences(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if(nums == null || nums.length < 2){
  5. return res;
  6. }
  7.  
  8. HashSet<List<Integer>> hs = new HashSet<>();
  9. dfs(nums, 0, new ArrayList<Integer>(), hs);
  10. return new ArrayList<List<Integer>>(hs);
  11. }
  12.  
  13. private void dfs(int [] nums, int start, List<Integer> item, Set<List<Integer>> hs){
  14. if(item.size() > 1){
  15. hs.add(new ArrayList<Integer>(item));
  16. }
  17.  
  18. for(int i = start; i<nums.length; i++){
  19. if(item.size() == 0 || item.get(item.size()-1) <= nums[i]){
  20. item.add(nums[i]);
  21. dfs(nums, i+1, item, hs);
  22. item.remove(item.size()-1);
  23. }
  24. }
  25. }
  26. }

Another way to avoid the duplicate is to record visited item on each level of DFS.

For the same level, if we see a previous visited number.

e.g. 4,6,7,7. when item = 4,6. First time visited 7, 7 is added to set. set = [6, 7]. item = 4,6,7. set is on this level of DFS.

Later it is removed from item, but it is still in the set. Thus when encountering the 2nd 7, it would skip.

But item = 4,6,7,7 still is added to res. That is because the second 7 is added to set on next level of DFS.

Time Complexity: exponential.

Space: O(n). n = nums.length.

AC Java:

  1. class Solution {
  2. public List<List<Integer>> findSubsequences(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if(nums == null || nums.length < 2){
  5. return res;
  6. }
  7.  
  8. dfs(nums, 0, new ArrayList<Integer>(), res);
  9. return res;
  10. }
  11.  
  12. private void dfs(int [] nums, int start, List<Integer> item, List<List<Integer>> res){
  13. if(item.size() > 1){
  14. res.add(new ArrayList<Integer>(item));
  15. }
  16.  
  17. HashSet<Integer> visited = new HashSet<Integer>();
  18. for(int i = start; i<nums.length; i++){
  19. if(visited.contains(nums[i])){
  20. continue;
  21. }
  22.  
  23. if(item.size() == 0 || item.get(item.size()-1) <= nums[i]){
  24. visited.add(nums[i]);
  25. item.add(nums[i]);
  26. dfs(nums, i+1, item, res);
  27. item.remove(item.size()-1);
  28. }
  29. }
  30. }
  31. }

LeetCode 491. Increasing Subsequences的更多相关文章

  1. [LeetCode] 491. Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

  2. 【LeetCode】491. Increasing Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 491. Increasing Subsequences增长型序列

    [抄题]: Given an integer array, your task is to find all the different possible increasing subsequence ...

  4. 【leetcode】491. Increasing Subsequences

    题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...

  5. 491. Increasing Subsequences

    这种increasing xxx 题真是老客户了.. 本题麻烦点在于不能重复, 但是和之前的那些 x sum的题目区别在于不能排序的 所以.... 我还是没搞定. 看了一个Java的思路是直接用set ...

  6. 491 Increasing Subsequences 递增子序列

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

  7. Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences)

    Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences) 深度优先搜索的解题详细介绍,点击 给定一个整型数组, 你的任务是找到所有该数组 ...

  8. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

  9. [Swift]LeetCode491. 递增子序列 | Increasing Subsequences

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

随机推荐

  1. idea(2018.3.5)破解

    第一步:下载idea,https://www.jetbrains.com/idea/download/#section=windows,双击进行安装 第二步:下载破解的jar包:链接:https:// ...

  2. jar包部署脚本

    部署一个名为xxx的jar包,输出到out.log,只需要准备以下脚本start.sh #!/bin/sh echo " =====关闭Java应用======" PROCESS= ...

  3. 简单实现python调用c#dll动态链接库

    在python调用c#dll库时要先安装库clr,即安装pythonnet,参考文章:https://www.cnblogs.com/kevin-Y/p/10235125.html(为在python中 ...

  4. java 基础 四种权限修饰符

    /** * Java有四种权限修饰符: * public > protected > (default) > private * 同一个类 YES YES YES YES * 同一个 ...

  5. C#读写修改设置调整UVC摄像头画面-清晰度

    有时,我们需要在C#代码中对摄像头的清晰度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄 ...

  6. [Linux] - 服务器/VPS一键检测带宽、CPU、内存、负载、IO读写

    一.SuperBench.sh VPS/服务器一键检测带宽.CPU.内存.负载.IO读写等的脚本: wget -qO- https://raw.githubusercontent.com/oooldk ...

  7. Vue自定义组件中Props中接收数组或对象

    原文:https://www.jianshu.com/p/904551dc6c15 自定义弹框组件时,需要在弹框内展示商品list,所以需要组件中的对应字段接收一个Array数组,默认返回空数组[], ...

  8. Spring AOP 复习

    Aspect Oriented Programming 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术,利用aop可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降 ...

  9. Java程序优化细节

    1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面:    1).控制资源的使用,通过线程同 ...

  10. Javascript中创建函数的几种方法

    // 工厂函数模式 // 无法解决对象识别问题 function person0(name, age, job) { var obj = new Object(); obj.name = name; ...