js给出所有组合

https://img2.mukewang.com/5c10bc410001c64705660085.jpg

如何得出多有组合?
用js
有好的思路指点一下吗~

湖上湖
浏览 566回答 1
1回答

守着星空守着你

考来的。。。function permute(input) {&nbsp; var permArr = [],&nbsp; usedChars = [];&nbsp; function main(input){&nbsp; &nbsp; var i, ch;&nbsp; &nbsp; for (i = 0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; ch = input.splice(i, 1)[0];&nbsp; &nbsp; &nbsp; usedChars.push(ch);&nbsp; &nbsp; &nbsp; if (input.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; permArr.push(usedChars.slice());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; main(input);&nbsp; &nbsp; &nbsp; input.splice(i, 0, ch);&nbsp; &nbsp; &nbsp; usedChars.pop();&nbsp; &nbsp; }&nbsp; &nbsp; return permArr;&nbsp; }&nbsp; return main(input).join('\n');};console.log(permute(['foo','bar','hello','world']));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript