作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/increasing-subsequences/description/

题目描述

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:

  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.

题目大意

找出一个数组中所有的递增的序列。

解题方法

我先写了dfs的做法,数组规模不超过15,那么O(N!)的时间复杂度能接受的。

dfs做法:

class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = set()
self.dfs(nums, 0, res, [])
return map(list, res) def dfs(self, nums, index, res, path):
if len(path) >= 2:
res.add(tuple(path))
for i in range(index, len(nums)):
if not path or nums[i] >= path[-1]:
self.dfs(nums, i + 1, res, path + [nums[i]])

下面是动态规划的代码,使用了一个set来保证不会有重复,然后由于set之中只能放不可变的对象,所以放进去的是元组对象。当我们遍历到nums的每个数字的时候,对set中的所有的元素进行遍历,看每个tuple元素的最后一个数字是否是小于等于当前的num的,如果是的话就把当前的元素添加到原来的tuple的后面。这样的循环虽说稍显复杂,但是竟然也能通过了。。

代码:

class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
dp = set()
for n in nums:
for y in list(dp):
if n >= y[-1]:
dp.add(y + (n,))
dp.add((n,))
return list(e for e in dp if len(e) > 1)

日期

2018 年 4 月 5 日 —— 清明节假期开始,小长假真好~~

【LeetCode】491. Increasing Subsequences 解题报告(Python)的更多相关文章

  1. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

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

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

  3. LeetCode 491. Increasing Subsequences

    原题链接在这里:https://leetcode.com/problems/increasing-subsequences/ 题目: Given an integer array, your task ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  5. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  7. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. svn简单上传下载文件命令

    上传命令: svn import 本地文件或目录 远程服务端目录 --username '用户名' --password '密码' -m '添加描述(可为空)' 下载命令: svn export 远程 ...

  2. 11-String to Integer (atoi)

    字符串转整型,更新之后的leetcode题,需考虑各种情况, 测试过标准库的atoi函数,当字符串越界返回的值是错的, 而该题要求越界时的返回边界值,更加严谨. Implement atoi to c ...

  3. 08 eclipse配置JDK

    eclipse配置JDK1.8 一.打开eclipse:Window>>Preferences: 二.搜索:"jdk",并点击右侧的"Add": 三 ...

  4. 学习java 7.19

    学习内容: 接口的组成中加入了默认方法,静态方法,私有方法 接口中默认方法:public default 返回值类型  方法名(参数列表){ } public default void show()  ...

  5. Shell【常用知识总结】

    一.常用知识总结 1.特殊变量($0,@,#,*,?) $0:当前脚本的文件名. $n:n是一个数字,表示第几个参数. $#:传递给脚本或函数的参数个数. $*:传递给脚本或函数的所有参数.当被双引号 ...

  6. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  7. mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'

    1. 问题:服务启动时,日志报错,导致启动失败: Caused by: com.mysql.cj.exceptions.CJException: null,  message from server: ...

  8. springboot 设置项目路劲后不能访问首页

    环境背景 学习版本 : springboot2.31 controller  代码 @controller public class Iindex{ @RequestMapping("/&q ...

  9. Mybatis-Plus默认主键策略导致自动生成19位长度主键id的坑

    原创/朱季谦 某天检查一位离职同事写的代码,发现其对应表虽然设置了AUTO_INCREMENT自增,但页面新增功能生成的数据主键id很诡异,长度达到了19位,且不是从1开始递增的-- 我检查了一下,发 ...

  10. Flink Exactly-once 实现原理解析

    关注公众号:大数据技术派,回复"资料",领取1024G资料. 这一课时我们将讲解 Flink "精确一次"的语义实现原理,同时这也是面试的必考点. Flink ...