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 ...
随机推荐
- Effective.Java第1-11条
1. 考虑使用静态工厂方法替代构造方法 一个类可以提供一个公共静态工厂方法,它只是返回类实例的静态方法.例如JDK的Boolean的valueOf方法: public final class Bool ...
- K8S学习笔记之k8s使用ceph实现动态持久化存储
0x00 概述 本文章介绍如何使用ceph为k8s提供动态申请pv的功能.ceph提供底层存储功能,cephfs方式支持k8s的pv的3种访问模式ReadWriteOnce,ReadOnlyMany ...
- laravel5.4 中 dd和dump的区别。
在laravel中dd和dump 都是打印的数据的,但是 dd会终止程序的运行,dump不会. 而且dump打印出来的数据在浏览器上是高亮的哦(很有逼格的~). 上图为dump打印出来的. 上图为dd ...
- C语言----选择结构(基础篇三)
大家好,忙里抽空更新一下自己的博客,算是自己的一个进步,C语言视频启蒙我早就看完啦,只是觉得这个视频真不错,所以给大家分享一下,同时自己还有很多没有理解透彻,写写博客算是一个笔记更是对自己所学的知识的 ...
- Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符)
Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符) 一丶多表查询 多表连接查询的应用场景: 连接是关系数据库模型的主要特点,也是区别于其他 ...
- Vue学习之全局和私有组件小结(七)
一.组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用相应的组件即可. 二.组件和模块: 1.模块化:是从代码逻 ...
- 分享整理vue.js在日常工作中用到的组件,帮助你在vue应用中快速开发
Vue-Echarts vue-echarts是封装后的vue插件, 基于 ECharts v4.0.1+ 开发,依赖 Vue.js v2.2.6+,功能一样的只是把它封装成vue插件 这样更方便以v ...
- mongoDB看这篇就够了
写在前面 hello,小伙伴们,我是 pubdreamcc ,本篇文章依旧出至于我的 GitHub仓库 node学习教程 ,如果你觉得我写的还不错,欢迎给个 star ,小伙伴们的 star 是我持续 ...
- C#8.0——异步流(AsyncStream)
异步流(AsyncStream) 原文地址:https://github.com/dotnet/roslyn/blob/master/docs/features/async-streams.md 注意 ...
- Filter和Listener
Filter: 1.概念: web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,做一些事. 过滤器的作用:一般用于完成一些通用的操作:登录验证.统一编码处理,敏感字符处理.... 2.快 ...