当前位置:网站首页>Leetcode491 increasing subsequence
Leetcode491 increasing subsequence
2022-06-12 17:59:00 【zhuxiaohai68】
Give you an array of integers nums , Find and return all different increasing subsequences in the array , In increasing subsequence There are at least two elements . You can press In any order Return to the answer .
The array may contain duplicate elements , If two integers are equal , It can also be regarded as a special case of increasing sequence .
Example 1:
Input :nums = [4,6,7,7]
Output :[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
Example 2:
Input :nums = [4,4,3,2,1]
Output :[[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)
# Satisfied handle path When adding a condition to the result , You can't return, because path You can continue to add
if len(path) >= 2:
result.append(path.copy())
if index >= n:
return
used_set = set()
for i in range(index, n):
# Generally, when there are repeated elements , It only needs nums[i]!=nums[i-1] To and fro
# The premise of this method is that the array implementation has been sorted ,
# So as long as the repeated items in the array will be next to each other .
# But here is the subsequence problem , Reorder not allowed , There can be any number of hops between elements , So as long as you have used it before
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()
边栏推荐
猜你喜欢

消息队列实战之队列优先级

Unprecedented analysis of Milvus source code architecture

Advanced mountain -asp Net core router basic use demo 0.1

Small program +app, a low-cost and active technology combination idea

Queue priority of message queue practice

C#操作数据库增查业务数据值内容案例学校表

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

First principles of enterprise architecture

Global lock, table lock, row lock

小程序+App,低成本获客及活跃的一种技术组合思路
随机推荐
Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl
First principles of enterprise architecture
Authorization in Golang ProjectUseing Casbin
Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data
Tutoriel de démarrage rapide JDBC
Message queuing MySQL tables that store message data
Figma从入门到放弃
Make good use of IDE, speed up R & D efficiency by 100%
leetcode 647. 回文子串
Vulnhub[DC3]
Vulnhub[DC3]
Two ways of tensorflow2 training data sets
channel原创
73. 矩阵置零(标记法)
数组按指定顺序排序
Figma from getting started to giving up
Introduction of one object one code tracing system
利用小程序快速生成App,只需七步
Applet and app are owned at the same time? A technical scheme with both
Codeforces Round #398 (Div. 2) D. Cartons of milk