扫码关注官方订阅号
如何得出多有组合?用js有好的思路指点一下吗~
考来的。。。
function permute(input) { var permArr = [], usedChars = []; function main(input){ var i, ch; for (i = 0; i < input.length; i++) { ch = input.splice(i, 1)[0]; usedChars.push(ch); if (input.length == 0) { permArr.push(usedChars.slice()); } main(input); input.splice(i, 0, ch); usedChars.pop(); } return permArr; } return main(input).join('\n'); }; console.log(permute(['foo','bar','hello','world']));
var arr = ['foo', 'bar', 'hello', 'world']; // 4 3 2 1 var first = {} var result = []; arr.forEach((first, idx, its) => { its.filter(e => e!==first).forEach((sec, idx, its) => { its.filter(e => e!==sec).forEach((third, idx, its)=> { its.filter(e => e!=third).forEach((forth, idx, its) => { console.log(first, sec, third, forth); result.push(first + sec + third + forth); }); }); }); });
= = 丧心病狂的遍历。。。
可以用有向图方法,得出四个节点所有路径
栈和队列
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
考来的。。。
= = 丧心病狂的遍历。。。
可以用有向图方法,得出四个节点所有路径
栈和队列