实现全排列

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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; j++) {

                for (var k = 0; k < preResult[j].length+1; k++) {

                    var temp=preResult[j].slice(0,k)+str[0]+preResult[j].slice(k);             

                    result.push(temp);             

                }

            }

        return result;

    

}

console.log(permutate("abc"));

https://www.cnblogs.com/star91/p/5615327.html

猜你喜欢

转载自blog.csdn.net/lgl_19910331/article/details/82682426