How to get the combination? Use jsDo you have any good ideas to give me~
Passed the exam. . .
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); }); }); }); });
= = Crazy traversal. . .
You can use the directed graph method to get all the paths of four nodes
Stacks and Queues
Passed the exam. . .
= = Crazy traversal. . .
You can use the directed graph method to get all the paths of four nodes
Stacks and Queues