-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 排列组合矩阵实现 #21
Comments
function cartesian (...lists) {
return lists
.map(list => list.map(item => [item]))
.reduce((listsA, listsB) =>
listsA.reduce((list, listA) =>
list.concat(
listsB.map(listB => listA.concat(listB))
), []
)
)
} |
(() => {
function getArranging (...list) {
return build(list)
}
function build (list, result = list.shift()) {
return !list.length ? result : build(list, [].concat(...list.shift().map(item =>
result.map(resultItem =>
[].concat(item, resultItem)
)
)))
}
let a = [0, 1]
let b = [3, 4, 5]
let c = [6, 7]
let ret = getArranging( a, b, c )
console.log(JSON.stringify(ret))
})() |
function getArranging(...args: number[][]) {
const result = [];
(function loop(tmp: number[] = []) {
if (tmp.length === args.length) {
result.push(tmp)
return
}
const values = args[tmp.length]
values.forEach(value => {
loop([...tmp, value])
})
})();
return result;
}
console.log(
getArranging([0, 1], [3, 4, 5], [6, 7])
); |
function getArranging(...list){ |
有这么几个变量:
a
其值可能为 0、1b
其值可能为 3、4、5c
其值可能为 6、7实现一个函数
getArranging
可以返回它们的可能值的排列组合二维数组:The text was updated successfully, but these errors were encountered: