思路:
非递减子序列 ,也是一种子集问题,也需要在进入递归就收集答案。这里需要注意,因为要求是序列,所以不能排序,这里需要使用set()在树层去重。同时,对于当前遍历到的元素,不能比path里面的小,如果小,则跳过该元素的搜索。
#非递减序列 也是一种子集问题
#有顺序要求,所以不能排序,要用set在树层去重
def f(nums):
res=[]
path=[]
n=len(nums)
def dfs(start_idx):
if len(path)>1: #长度大于2才收集
res.append(path[:])
st=set()
for i in range(start_idx,n):
if (path and nums[i]<path[-1]) or nums[i] in st: #去重逻辑
continue
st.add(nums[i])
path.append(nums[i])
dfs(i+1)
path.pop()
dfs(0)
print(res)
return res
def main():
nums=list(map(int,input().split()))
f(nums)
if __name__=="__main__":
main()