【发布时间】:2020-06-01 14:37:03
【问题描述】:
假设我们有这个数组:
let a = ["a", "b", "c"]
我需要一些如下组合:
[["a"], ["b"], ["c"], ["a", "b"], ["a", "c"], ["b", "c"], ["b", "a"], ["c", "a"], ["c","b"], ["a", "b", "c"], ...]
const a = ["a", "b", "c"];
function perm(xs) {
let ret = [];
for (let i = 0; i < xs.length; i = i + 1) {
let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1)));
if (!rest.length) {
ret.push([xs[i]])
} else {
for (let j = 0; j < rest.length; j = j + 1) {
ret.push([xs[i]].concat(rest[j]))
}
}
}
return ret;
}
console.log(perm(a));
随意编辑这个问题,如果这是一个类似的问题,请复制这个问题。strong text
【问题讨论】:
-
php 版本可以在这里找到:stackoverflow.com/a/61488013/3125277
标签: javascript