/**
* 递归排列
* 从 arr[1...n] 中任选 num(0 < num <= n) 个数的所有排列
*/
function recursion_permutate(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i < l; i++) {
f(t.concat(a[i]), a.slice(0, i).concat(a.slice(i + 1)), n - 1);
}
})([], arr, num);
return r;
}
/**
* 递归组合
* 从 arr[1...n] 中任选 num(0 < num <= n) 个数的所有组合
*/
function recursion_combine(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i <= l - n; i++) {
f(t.concat(a[i]), a.slice(i + 1), n - 1);
}
})([], arr, num);
return r;
}
转载于:https://www.cnblogs.com/xiaohui108/archive/2011/10/17/2215534.html