Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the full permutation output algorithm of array by JS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report