Python版
# Python3.6+
def subsets(xs: list) -> list[list]:
def select(es: list, n: int) -> list[list]:
if len(es) < n:
return []
return select(es[:-1], n) + [h+[es[-1]] for h in select(es[:-1], n-1)] if n else [[]]
return [select(xs, i+1) for i in range(len(xs))]
subsets(list('abcd'))
Out[2]:
[[['a'], ['b'], ['c'], ['d']],
[['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'd'], ['b', 'd'], ['c', 'd']],
[['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'c', 'd'], ['b', 'c', 'd']],
[['a', 'b', 'c', 'd']]]
【 在 iStudy 的大作中提到: 】
: 发信人: iStudy (爱学习), 信区: Java
: 标 题: 这种组合算法怎么写?
: 发信站: 水木社区 (Tue May 28 19:10:33 2024), 站内
: ...................
--
FROM 223.166.244.*