给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。
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.
示例:
输入: [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]]
说明 :
- 给定数组的长度不会超过15。
- 数组中的整数范围是 [-100,100]。
- 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。
思路(来源):
- 动态规划
- 深度优先搜索 DFS(递归)
- 广度优先搜索 BFS(队列)
Python
# 动态规划
def findSubsequences(nums):
if not nums:
return []
# 用集合来存储所有可能符合条件的子字符串
res = {
(nums[0],)}
for n in nums[1:]:
res.update({
j+(n,) for j in res if n >= j[-1]})
res.add((n,))
return [list(i) for i in res