491. 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
排序没用,同一个7会被算2次,出现2个[4,7]。所以要用set<list>去重
[英文数据结构或算法,为什么不用别的数据结构或算法]:
新建数组,里面的参数是集合就行 很随意
new ArrayList(res);
[一句话思路]:
backtracing的函数里必须把数组完全地for一遍,否则不算完全的深度搜索。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 主函数里先对新变量参数命好名,求结果时可以直接拿出来用
- cur.size() >= 2时即可回收
[二刷]:
括号里参数传list的时候,必须写new ArrayList(set)
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
backtracing的函数里必须把数组完全地for一遍,否则不算完全的深度搜索。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
// package whatever; // don't place package name! import java.io.*;
import java.util.*;
import java.lang.*; class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
//initialization: result, set
List<Integer> cur = new ArrayList<Integer>();
Set<List<Integer>> set = new HashSet<>(); //dfs
dfs(0, nums, cur, set); //return result
List<List<Integer>> result = new ArrayList(new ArrayList(set));
return result;
} public void dfs(int index, int[] nums, List<Integer> cur, Set<List<Integer>> set) {
//add if cur.size() >= 2
if (cur.size() >= 2) set.add(new ArrayList(cur)); //for each number in nums, do backtracing
for (int i = index; i < nums.length; i++) {
//add to cur if cur is null or the next num is bigger
if (cur.size() == 0 || nums[i] >= cur.get(cur.size() - 1)) {
cur.add(nums[i]);
dfs(i + 1, nums, cur, set);
cur.remove(cur.size() - 1);
}
}
}
} class driverFuction {
public static void main (String[] args) {
Solution answer = new Solution();
int[] nums = {4, 6, 7, 7};
List<List<Integer>> result = answer.findSubsequences(nums);
System.out.println(result);
}
}
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 ...
- LeetCode 491. Increasing Subsequences
原题链接在这里:https://leetcode.com/problems/increasing-subsequences/ 题目: Given an integer array, your task ...
- 491. Increasing Subsequences
这种increasing xxx 题真是老客户了.. 本题麻烦点在于不能重复, 但是和之前的那些 x sum的题目区别在于不能排序的 所以.... 我还是没搞定. 看了一个Java的思路是直接用set ...
- 【leetcode】491. Increasing Subsequences
题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...
- 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 ...
- SnackDown Longest Increasing Subsequences 构造题
Longest Increasing Subsequences 题目连接: https://www.codechef.com/SNCKPA16/problems/MAKELIS Description ...
随机推荐
- mac添加redis 环境变量
cd /etc/paths.d touch redis vim redis 写入 /Users/love/Downloads/redis-4.0.10/src 之后就可以直接执行redis-cli r ...
- linux环境下安装nginx步骤(不错)
开始前,请确认gcc g++开发类库是否装好,默认已经安装. ububtu平台编译环境可以使用以下指令 apt-get install build-essential apt-get install ...
- Java中的接口和抽象类
接口和抽象类是Java设计中最基本的概念,它们都不能实例化对象,都可以实现多态,也都能用来创建匿名内部类.但实际使用上还有很多的不同. 两者的语法定义不同,对应的设计抽象关系也不同,接口主要是对行为的 ...
- Linux内核分析第六次作业
分析system_call中断处理过程 一.先在实验楼的虚拟机中MenuOs增加utsname和utsname-asm指令. 具体实现如下: 1.克隆最新新版本的menu,之后进入menu 2.进入t ...
- pycharm同步代码
有时候我们需要在windows环境开发代码,但是程序要跑在linux服务器或者我们的linux虚拟机上,这就设计到了代码同步的问题. pycharm提供了代码同步的功能. 在导航栏点击Tools--D ...
- [蓝桥杯]ALGO-20.算法训练_求先序排列
问题描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=). 输入格式 两行,每行一个字符串,分别表示中序和后序排列 输出格式 一个字符串,表示所求 ...
- 廖雪峰Java6 IO编程-2input和output-7序列化
1.序列化 序列化是指把一个Java对象变成二进制内容byte[] 序列化后可以把byte[]保存到文件中 序列化后可以把byte[]通过网络传输 一个Java对象要能序列化,必须实现Serializ ...
- HDFS知识点总结
学习完Hadoop权威指南有一段时间了,现在再回顾和总结一下HDFS的知识点. 1.HDFS的设计 HDFS是什么:HDFS即Hadoop分布式文件系统(Hadoop Distributed File ...
- 支付宝 net
- 【Python】pip国内安装源和yum恢复
豆瓣安装源 pip install packages -i http://pypi.doubanio.com/simple --upgrade --trusted-host pypi.doubanio ...