题目描述
给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是 2 。
示例:
输入:[4, 6, 7, 7]
输出:[[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
输入:[4, 2, 3, 1]
输出:[[2,3]]
示例代码:
class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
total_result = []
nums_len = len(nums)
def dfs(begin, result):
if begin == nums_len:
return
used_nums = {}
for i in range(begin, nums_len):
if nums[i] not in used_nums:
used_nums[nums[i]] = None
else:
continue
if result and result[-1] > nums[i]:
continue
result.append(nums[i])
if len(result) >= 2:
total_result.append(result[:])
dfs(i+1, result)
result.pop()
dfs(0, [])
return total_result