警告
如果没有看到您的算法及其任何输入限制(例如,集合中是否允许重复值?),可能无法设计出适用于所有可能情况的方法。
但是,以下内容似乎适用于您的示例结果表中列出的所有情况。如果您发现这不适用于其他情况,请将这些示例添加到您的问题中。 (我忽略了 target = 0 作为特例。)
算法草图
按升序遍历目标列的结果。
当结果增加时,您已经确定了另一个子集,并在该子集中找到了最大值。
要查找子集中的剩余值,请以店员的方式“进行更改”。换句话说,沿着设定值往回走,尽可能从剩余的总数中减去。
样品运行
对于集合 [1,3,5,7,9,10] 中子集的目标总和 = 13 的给定示例:
resultCount = 0
result(13,1) is 0; this result - resultCount = 0, so no new subsets
result(13,3) is 0; this result - resultCount = 0, so no new subsets
result(13,5) is 0; this result - resultCount = 0, so no new subsets
result(13,7) is 1; this result - resultCount = 1, so resultCount = 1
and new subset = [7]
and remaining = 13 - 7 = 6
5 < 6, so subset = [7,5] and remaining = 6 - 5 = 1
3 > 1, so remaining is still 1
1 = 1, so subset = [7,5,1] and remaining = 0 (subset complete)
result(13,9) is 2; this result - resultCount = 1, so resultCount = 2
and new subset = [9]
and remaining = 13 - 9 = 4
7 > 4, so remaining is still 4
5 > 4, so remaining is still 4
3 < 4, so subset = [9,3] and remaining = 4 - 3 = 1
1 = 1, so subset = [9,3,1] and remaining = 1 - 1 = 0 (subset complete)
result(13,10) is 3; this result - resultCount = 1, so resultCount = 3
and new subset = [10]
and remaining = 13 - 10 = 3
9 > 3, so remaining is still 3
7 > 3, so remaining is still 3
5 > 3, so remaining is still 3
3 = 3, so subset = [10,3] and remaining = 3 - 3 = 0 (subset complete)
识别出 3 个子集的运行结束:
[7,5,1]
[9,3,1]
[10,3]