当前位置:网站首页>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()
边栏推荐
- 有关cookie的用法介绍
- Use of split method of string
- Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
- Idea common shortcut keys
- 续2 asp.net core 路由程序基础使用演示0.2 默认控制器数据的获取到
- JDBC几个坑
- 数据库SQL操作基础
- Yyds dry goods inventory leetcode question set 911 - 920
- 低代码平台调研结果
- MIPS 通用寄存器 + 指令
猜你喜欢
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
App中快速复用微信登录授权的一种方法
Tensorflow prompts typeerror: unsupported operand type (s) for *: 'float' and 'nonetype‘
Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
Hangzhou AI developer meetup registration opens!
Office application cannot start normally 0xc0000142
vant3+ts h5页面嵌套进app 与原生app通信
1.5 什么是架构师(连载)
Vant3+ts H5 pages are nested into apps to communicate with native apps
随机推荐
Arm64 Stack backtrack
C operation database added business data value content case school table
Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data
Reconnaître l'originalité de la fonction
小程序和App同时拥有?两者兼得的一种技术方案
Vulnhub[DC3]
Arm64棧回溯
Evolution and thinking of Taobao native R & D mode | DX R & D mode
String s = null ; String s = new String(); String s = "; what is the difference between string s?
Unprecedented analysis of Milvus source code architecture
JDBC quick start tutorial
进阶之大山-asp.net core 路由程序基础使用演示0.1
SSM集成FreeMarker以及常用语法
Introduction of one object one code tracing system
Tensorflow prompts typeerror: unsupported operand type (s) for *: 'float' and 'nonetype‘
Array sorts in the specified order
Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
SQL游标(cursor)详细说明及内部循环使用示例
TensorFlow从网络读取数据
SSM integrates FreeMarker and common syntax