LeetCode 491. Increasing Subsequences
原题链接在这里: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:
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:
- The length of the given array will not exceed 15.
- The range of integer in the given array is [-100,100].
- 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:
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length < 2){
return res;
} HashSet<List<Integer>> hs = new HashSet<>();
dfs(nums, 0, new ArrayList<Integer>(), hs);
return new ArrayList<List<Integer>>(hs);
} private void dfs(int [] nums, int start, List<Integer> item, Set<List<Integer>> hs){
if(item.size() > 1){
hs.add(new ArrayList<Integer>(item));
} for(int i = start; i<nums.length; i++){
if(item.size() == 0 || item.get(item.size()-1) <= nums[i]){
item.add(nums[i]);
dfs(nums, i+1, item, hs);
item.remove(item.size()-1);
}
}
}
}
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:
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length < 2){
return res;
} dfs(nums, 0, new ArrayList<Integer>(), res);
return res;
} private void dfs(int [] nums, int start, List<Integer> item, List<List<Integer>> res){
if(item.size() > 1){
res.add(new ArrayList<Integer>(item));
} HashSet<Integer> visited = new HashSet<Integer>();
for(int i = start; i<nums.length; i++){
if(visited.contains(nums[i])){
continue;
} if(item.size() == 0 || item.get(item.size()-1) <= nums[i]){
visited.add(nums[i]);
item.add(nums[i]);
dfs(nums, i+1, item, res);
item.remove(item.size()-1);
}
}
}
}
LeetCode 491. Increasing Subsequences的更多相关文章
- [LeetCode] 491. Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- 【LeetCode】491. Increasing Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 491. Increasing Subsequences增长型序列
[抄题]: Given an integer array, your task is to find all the different possible increasing subsequence ...
- 【leetcode】491. Increasing Subsequences
题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...
- 491. Increasing Subsequences
这种increasing xxx 题真是老客户了.. 本题麻烦点在于不能重复, 但是和之前的那些 x sum的题目区别在于不能排序的 所以.... 我还是没搞定. 看了一个Java的思路是直接用set ...
- 491 Increasing Subsequences 递增子序列
给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2.示例:输入: [4, 6, 7, 7]输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, ...
- Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences)
Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences) 深度优先搜索的解题详细介绍,点击 给定一个整型数组, 你的任务是找到所有该数组 ...
- [LeetCode] Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- [Swift]LeetCode491. 递增子序列 | Increasing Subsequences
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
随机推荐
- 『金字塔 区间dp』
金字塔 Description 虽然探索金字塔是极其老套的剧情,但是这一队 探险家还是到了某金字塔脚下.经过多年的研究,科 学家对这座金字塔的内部结构已经有所了解.首先, 金字塔由若干房间组成,房间之 ...
- Java的jdk环境变量配置
方法/步骤 1.安装JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹中.(不能都安装在java ...
- 命令创建.net core3.0 web应用详解(超详细教程)
原文:命令创建.net core3.0 web应用详解(超详细教程) 你是不是曾经膜拜那些敲几行代码就可以创建项目的大神,学习了命令创建项目你也可以成为大神,其实命令创建项目很简单. 1.cmd命令行 ...
- asp.net core 系列之Reponse caching 之 Response Caching Middleware(4)
这篇文章介绍 Response Caching Middleware . Response Caching Middleware in ASP.NET Core 通过在ASP.NET Core应用中 ...
- java 泛型和object比较
引言 我们使用object和泛型做形参,都是为了让这个方法能接收更多类型的对象,让程序变得更健壮,代码复用率更高.当我们回看自己写的代码时会发现,好像使用泛型的地方使用object也可以,使用obje ...
- Golang中文乱码问题
在学习golang读取文件的过程中,遇到中文显示乱码的问题!golang没有自带的编解码包,因此需要借助第三方包 解决方法: 引入第三发转码包:git clone https://github.com ...
- 【python】多任务(1. 线程)
线程执行的顺序是不确定,可以通过适当的延时,保证某一线程先执行 基础语法 # 多线程的使用方式 import threading def test1():... # 如果创建Thread时执行的函数, ...
- pandas 之 多层索引
In many applications, data may be spread across a number of files or datasets or be arranged in a fo ...
- Win10下免安装版JDK8环境变量配置
1.解压JDK 2.配置JAVA_HOME环境变量 D:\Free\jdk1.8.0_92 3.配置CLASSPATH环境变量 .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\to ...
- 记使用pyspider时,任务不执行的问题原因:save太大。
pyspider使用save传递大量文本时,如果是mysql数据库,有可能出现问题,因为任务表默认用的blob字段.字符数是有限制的. 解决办法就是手动把字段类型改成longblob. 希望作者能直接 ...