python-找出数组的解集题目

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明:
所有数字(包括 target)都是正整数。解集不能包含重复的组合。
输入:candidates = [2,3,6,7] target = 7所求解集为:
[7],
[2,2,3]

import itertools as it

candidates = [2, 3, 6, 7, 4, 5]
target = 9

res = set()
for i in range(1, len(candidates) + 1):
    x = it.combinations(candidates, i)
    for j in x:
        if sum(j) == target:            
            res.add(tuple(sorted(j)))
        else:
            dd = target - sum(j)
            if dd <= 0:continue
            for i in candidates:
                yy = dd % i
                if yy == 0 and dd // i != 1:
                    res.add(tuple(sorted(list(j) + [i] * (dd // i))))
    
print(res)