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 ...
随机推荐
- 解决:ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];
简记 使用SkyWalking用ES做存储,发现运行一段时间会提示ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index ...
- golang学习笔记--包导入及go 常用命令及参数
包导入:包导入路劲即代码包在工作区的src目录下的相对路径. 同一个源码文件中导入的多个代码包的最后一个元素不能重复,否则引起编译错误,如果只导入不使用,同样会引起编译错误 若想导入最后一个元素名相同 ...
- opencv imshow plt imshow
opencv官方文档上写的,https://docs.opencv.org/master/dc/d2e/tutorial_py_image_display.html Color image loade ...
- python基础09--闭包,装饰器
1.1 闭包 1.函数作为第一类对象,支持赋值给变量,作为参数传递给其它函数,作为其它函数的返回值,支持函数的嵌套,实现了__call__方法的类实例对象也可以当做函数被调用 2.s= func - ...
- Python进阶----pymysql模块的使用,单表查询
Python进阶----pymysql模块的使用,单表查询 一丶使用pymysql 1.下载pymysql包: pip3 install pymysql 2.编写代码 ...
- 【Kafka】Exactly Once语义与事务
Kafka在0.11.0.0之前的版本中只支持At Least Once和At Most Once语义,尚不支持Exactly Once语义. 但是在很多要求严格的场景下,如使用Kafka处理交易数据 ...
- js进度条源码下载—js进度条代码
现在很多网站会用到进入网站特效,到网页没有加载完成的时候,会有一个loding特效,加载完了之后才能看到页面,今天就带着做一个js进度条效果,今天要做的效果是纯js进度条加载,没有用到框架,方便大家进 ...
- 英文finaunce金融
金融 1.指货币的发行.流通和回笼,贷款的发放和收回,存款的存入和提取,汇兑的往来等经济活动. 胡适<国际的中国>:“我们更想想这几年国内的资产阶级,为了贪图高利债的利益,拚命的借债给中国 ...
- Node初识
初识Nodejs Node.js的诞生 作者Ryan Dahl 瑞恩·达尔 2004 纽约 读数学博士 2006 退学到智利 转向开发 2009.5对外宣布node项目,年底js大会发表演讲 2010 ...
- 一次 Young GC 的优化实践(FinalReference 相关)
本文转载自公众号:涤生的博客,阅读时间大约需要11分钟.涤生的文章看起来跟破案一样,很精彩,很有启发. 前言 博客已经好久没有更新了,主要原因是 18 年下半年工作比较忙,另外也没有比较有意思的题材, ...