具有不同长度和不重复的组合
任何人都可以提出一个简单的方法来获得所有组合而不重复,并且长度可变吗?
[0,1,2,3,4]
(2's):[0,1],[0,2],[0,3] ... [3,4]
(3's):[0,1,2],[0,1,3] ... [2,3,4]
(4's):[0,1,2,3],[0,1,2,4] ... [1,2,3,4]
花了我一段时间,但我想我已经在这里了...
这有所有的组合没有重复
var arr:Array = [0, 1, 2, 3, 4];
var $allcombos:Array = [];
findCombos($allcombos,[],arr);
function findCombos($root:Array, $base:Array, $rem:Array):void {
for (var i:int = 0; i < $rem.length; i++) {
var a:Array = $base.concat();
a.push($rem[i]);
findCombos($root, a, $rem.slice(i + 1));
if (a.length > 1) {
$root.push(a);
}
}
}
链接地址: http://www.djcxy.com/p/85025.html
上一篇: combinations with varying length and without repetition