combinations with varying length and without repetition
Can anyone suggest an easy way of getting all combinations without repeats, and with a varying length ?
[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]
took me a while but I think I've got it here...
this has all the combinations without repeats
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/85026.html
上一篇: Php递归获得所有可能的字符串
下一篇: 具有不同长度和不重复的组合