当前位置:网站首页>leetcode491 递增子序列
leetcode491 递增子序列
2022-06-12 17:51:00 【zhuxiaohai68】
给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。
数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。
示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]
class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
result = list()
self.dfs(nums, result, list(), 0)
return result
def dfs(self, nums, result, path, index):
n = len(nums)
# 满足把path添加到结果的条件的时候,不能return,因为path后还可以继续添加
if len(path) >= 2:
result.append(path.copy())
if index >= n:
return
used_set = set()
for i in range(index, n):
# 一般的含有重复元素的时候,只需要nums[i]!=nums[i-1]来去重
# 这个方法的前提是数组实现已经排序过了,
# 因此只要数组里面重复的东西都会挨在一起。
# 但是这里是子序列问题,不允许重新排序,元素之间可以有任意多跳,因此只要以前使用过都不行
if nums[i] in used_set:
continue
if (len(path) == 0) or (nums[i] >= path[-1]):
used_set.add(nums[i])
path.append(nums[i])
self.dfs(nums, result, path, i + 1)
path.pop()
边栏推荐
- 118. 杨辉三角(动态规划)
- New media operation material website sharing enables you to create current affairs with half the effort
- 認識函數原創
- [csp]202012-2 optimal threshold for period end forecast
- C operation database added business data value content case school table
- qemu+gdb小节
- 徽商期货公司开户可靠,交易安全吗?
- 全局锁、表锁、行锁
- Goframe gredis configuration management | comparison of configuration files and configuration methods
- SQL游标(cursor)详细说明及内部循环使用示例
猜你喜欢

TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘

《用户体验要素:以用户为中心的产品设计》笔记

Advanced mountain -asp Net core router basic use demo 0.1

Office application cannot start normally 0xc0000142

PHP implementation of infinite classification tree (recursion and Optimization)

Applet and app are owned at the same time? A technical scheme with both

Cesium抛物线方程

论文《Deep Interest Evolution Network for Click-Through Rate Prediction》

The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time zone. ......

Lightweight and convenient small program to app technology solution to realize interconnection with wechat / traffic app
随机推荐
Use the official go Library of mongodb to operate the original mongodb
Vant3+ts H5 pages are nested into apps to communicate with native apps
Small program +app, a low-cost and active technology combination idea
迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
vant3+ts h5页面嵌套进app 与原生app通信
Tcp/ip family structure and protocol of tcp/ip series overview
Make good use of IDE, speed up R & D efficiency by 100%
字节飞书人力资源套件三面
Vant3 +ts packaged simple step advancer component
一种好用、易上手的小程序IDE
Lightweight and convenient small program to app technology solution to realize interconnection with wechat / traffic app
Yyds dry goods inventory leetcode question set 911 - 920
TensorFlow从网络读取数据
Are Huishang futures accounts reliable? Is the fund safe?
Figma从入门到放弃
error接口原创
String s = null ; String s = new String(); String s = "; what is the difference between string s?
PHP implementation of infinite classification tree (recursion and Optimization)
JDBC几个坑
Cesium parabolic equation