leetcode 491 递增子序列
z

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是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]。
给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。
链接:https://leetcode-cn.com/problems/increasing-subsequences

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
ans = []
self.bfs(nums, 0, len(nums), ans, [])
return ans


def bfs(self, nums, start, end, ans, temp):
if start <= end and len(temp) >= 2:
if temp not in ans:
ans.append(copy.copy(temp))

for i in range(start, end):
if len(temp) > 0 and nums[i] >= temp[-1] or len(temp) == 0:
temp.append(nums[i])
self.bfs(nums, i+1, end, ans, temp)
temp.remove(nums[i])