首先我有一个数组,像这样:
var arr = [[2,3,4,5,6,7,8,9,10],
[3,4,5,6,7,8,9,10,11],
[4,5,6,7,8,9,10,11,12]]
不一定是这个数组,也可以是其他数组,只是举例。所以我需要知道所有可能排列的数量,如下所示:
2,3,4
2,3,5
...
2,3,12
2,4,5
2,4,6
...
2,4,12
...
10,11,12
据我所知,这不是简单的排列。而且我不需要所有这些组合,我只需要它们的数量,可能是存在的公式或类似的东西。在这个例子中,我知道答案是 165。谢谢
最佳答案
假设订单必须增加:
var arr = [[2,3,4,5,6,7,8,9,10],
[3,4,5,6,7,8,9,10,11],
[4,5,6,7,8,9,10,11,12]];
function count(stack, history) {
var counter = 0;
history = history || [];
stack[0].forEach(function(it) {
if (! history.length || history[history.length -1] < it) {
if (stack[1]) {
counter += count(stack.slice(1), history.concat([it]));
} else {
counter++;
}
}
});
return counter;
}
console.log(count(arr)); //165
关于javascript - 通过 Javascript 计算数组中排列的计数而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22591061/