How to realize the full permutation output algorithm of array by JS
This article mainly shows you "JS how to achieve array full permutation output algorithm", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "JS how to achieve array full permutation output algorithm" this article.
The sample code is as follows:
Function permute (input) {var permArr = [], usedChars = []; function main (input) {var I, ch; for (I = 0; I < input.length; iTunes +) {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);}; console.log (permute ([5,3,7,1]))
The other is to sort by string.
Function permutate (str) {var result= []; if (str.length==1) {return [str]} else {var preResult=permutate (str.slice (1)); for (var j = 0; j < preResult.length; jlength +) {for (var k = 0; k < preResult [j] .length + 1) Temp +) {var temp= preResult [j] .slice (0djk) + str [0] + preResult.slice (k); result.push (temp);}} return result;}} console.log (permutate ("abc")) These are all the contents of the article "how to implement the array full permutation output algorithm in JS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!