function getGroup(arr) { let r = []; function f(n){ if(n==0){ r.push([arr[0]]); return r; } f(n-1).forEach(v=>{ v = v.slice(); v.push(arr[n]) r.push(v); }); r.push([arr[n]]); return r; } return f(arr.length-1); }
输入:[1,2,3]
输出:[ [ 1 ], [ 1, 2 ], [ 2 ], [ 1, 3 ], [ 1, 2, 3 ], [ 2, 3 ], [ 3 ] ]
function getGroupStr(str) { let r = []; function f(n){ if(n==0){ r.push(str[0]); return r; } f(n-1).forEach(v=>{ r.push(v+str[n]); }); r.push(str[n]); return r; } return f(str.length-1); }
输入:“abc”
输出:['a', 'ab', 'b', 'ac', 'abc', 'bc', 'c']
// 返回分组的索引 function groupSplitIdx (arrSize, n) { var r = []; function _(t, i) { if (t.length === n) { r[r.length] = t.slice(); return; } for (; i < arrSize; i++) { t.push(i); _(t, i + 1); t.pop(); } } _([], 0); return r; }
输入:5,3
输出:
[ [ 0, 1, 2 ], [ 0, 1, 3 ], [ 0, 1, 4 ], [ 0, 2, 3 ], [ 0, 2, 4 ], [ 0, 3, 4 ], [ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 3, 4 ], [ 2, 3, 4 ] ]
function groupSplit (arr, size) { var r = []; let b = new Array(size); function _(start, end, x) { if (x == size) { r.push(b.slice()); return; } if ((end - start) < (size - x)) { return; } b[x] = arr[start]; _(start + 1, end, x + 1); _(start + 1, end, x); _(0, arr.length, 0); return r; }
输入:[1,2,3,4,5], 3
输出:
[ [ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 2, 5 ], [ 1, 3, 4 ], [ 1, 3, 5 ], [ 1, 4, 5 ], [ 2, 3, 4 ], [ 2, 3, 5 ], [ 2, 4, 5 ], [ 3, 4, 5 ] ]
function groupSplitIdx2 (m) { let r = []; function _(i) { if (i == 0) { r.push([0]); return r; } _(i - 1).forEach(v=>{ v = v.slice(); v.push(i); r.push(v); }); r.push([i]); return r; } return _(m-1); }
输入:4
输出:
[ [ 0 ], [ 0, 1 ], [ 1 ], [ 0, 2 ], [ 0, 1, 2 ], [ 1, 2 ], [ 2 ], [ 0, 3 ], [ 0, 1, 3 ], [ 1, 3 ], [ 0, 2, 3 ], [ 0, 1, 2, 3 ], [ 1, 2, 3 ], [ 2, 3 ], [ 3 ] ]
function groupSplitIdx3 (arrSize, n) { var r = []; function _(t, i) { if (t.length === n) { return; } for (; i < arrSize; i++) { t.push(i); r.push(t.slice()); _(t, i + 1); t.pop(); } } _([], 0); return r; }
输入:5,3
输出:
[ [ 0 ], [ 0, 1 ], [ 0, 1, 2 ], [ 0, 1, 3 ], [ 0, 1, 4 ], [ 0, 2 ], [ 0, 2, 3 ], [ 0, 2, 4 ], [ 0, 3 ], [ 0, 3, 4 ], [ 0, 4 ], [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 3 ], [ 1, 3, 4 ], [ 1, 4 ], [ 2 ], [ 2, 3 ], [ 2, 3, 4 ], [ 2, 4 ], [ 3 ], [ 3, 4 ], [ 4 ] ]
数组的排列组合
function arrCom(arr) { function _(n) { if (n === 1) { return arr[0].map(a=>[a]); } else { let r = []; _(n - 1).forEach(item=>{ arr[n - 1].forEach(a=>{ let item2 = item.slice(); item2.push(a); r.push(item2); }); }); return r; } } return _(arr.length); }
输入:
[
[1, 2], [3, 4], [5, 6, 7, 9, 10]
]
输出:
[
[ 1, 3, 5 ], [ 1, 3, 6 ],
[ 1, 3, 7 ], [ 1, 3, 9 ],
[ 1, 3, 10 ], [ 1, 4, 5 ],
[ 1, 4, 6 ], [ 1, 4, 7 ],
[ 1, 4, 9 ], [ 1, 4, 10 ],
[ 2, 3, 5 ], [ 2, 3, 6 ],
[ 2, 3, 7 ], [ 2, 3, 9 ],
[ 2, 3, 10 ], [ 2, 4, 5 ],
[ 2, 4, 6 ], [ 2, 4, 7 ],
[ 2, 4, 9 ], [ 2, 4, 10 ]
]
快速组合的方式:
function arrCom(arr, cb) { let r = []; let lastIdx = arr.length - 1; function _(arr, n) { let a = arr[n]; if (n === lastIdx) { if (a.length) { for (let i = 0, j = a.length; i < j; i++) { r.push(a[i]); cb(r); r.pop(); } } else { cb(r); } return; } if (a.length) { for (let i = 0, j = a.length; i < j; i++) { r.push(a[i]); _(arr, n + 1); r.pop(); } } else { _(arr, n + 1); } } _(arr, 0); }