JavaScript 전체 배열 의 6 가지 알고리즘 이 구체 적 으로 실현 되 었 습 니 다.
                                            
 5843 단어  JavaScript전체 배열알고리즘
                    
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>Full Permutation(Recursive Swap) - Mengliao Software</title>  
</head>  
<body>  
<p>Full Permutation(Recursive Swap)<br />  
Mengliao Software Studio - Bosun Network Co., Ltd.<br />  
2011.05.24</p>  
<script type="text/javascript">  
/*  
   (    )    
1、                 ;  
2、           (  );  
3、                。  
*/ 
function swap(arr,i,j) {  
    if(i!=j) {  
        var temp=arr[i];  
        arr[i]=arr[j];  
        arr[j]=temp;  
    }  
}  
var count=0;  
function show(arr) {  
    document.write("P<sub>"+ ++count+"</sub>: "+arr+"<br />");  
}  
function perm(arr) {  
    (function fn(n) { //  n         
        for(var i=n;i<arr.length;i++) {  
            swap(arr,i,n);  
            if(n+1<arr.length-1) //                   1   
                fn(n+1); //  n+1          
            else 
                show(arr); //        
            swap(arr,i,n);  
        }  
    })(0);  
}  
perm(["e1","e2","e3","e4"]);  
</script>  
</body>  
</html> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>Full Permutation(Recursive Link) - Mengliao Software</title>  
</head>  
<body>  
<p>Full Permutation(Recursive Link)<br />  
Mengliao Software Studio - Bosun Network Co., Ltd.<br />  
2012.03.29</p>  
<script type="text/javascript">  
/*  
   (    )    
1、          ,          (       );  
2、                   (       );  
3、             (       );  
4、                     2、3,       ,       。  
*/ 
var count=0;  
function show(arr) {  
    document.write("P<sub>"+ ++count+"</sub>: "+arr+"<br />");  
}  
function perm(arr) {  
    (function fn(source, result) {  
        if (source.length == 0)  
            show(result);  
        else 
            for (var i = 0; i < source.length; i++)  
                fn(source.slice(0, i).concat(source.slice(i + 1)), result.concat(source[i]));  
    })(arr, []);  
}  
perm(["e1", "e2", "e3", "e4"]);  
</script>  
</body>  
</html> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>Full Permutation(Recursive Backtrack) - Mengliao Software</title>  
</head>  
<body>  
<p>Full Permutation(Recursive Backtrack)<br />  
Mengliao Software Studio - Bosun Network Co., Ltd.<br />  
2012.03.29</p>  
<script type="text/javascript">  
/*  
   (    )    
1、      ,        ,             ;  
2、      ,     n   ;  
3、 n               。  
*/ 
var count = 0;  
function show(arr) {  
    document.write("P<sub>" + ++count + "</sub>: " + arr + "<br />");  
}  
function seek(index, n) {  
    if (n >= 0) //                ,              
        if (index[n] < index.length - 1) { //           
            index[n]++; //         
            if ((function () { //                    
                for (var i = 0; i < n; i++)  
                    if (index[i] == index[n]) return true; //     
                return false; //     
            })())  
                return seek(index, n); //       
            else 
                return true; //    
        }  
        else { //       ,        
            index[n] = -1; //        
            if (seek(index, n - 1)) //          
                return seek(index, n); //         
            else 
                return false; //        
        }  
    else 
        return false;  
}  
function perm(arr) {  
    var index = new Array(arr.length);  
    for (var i = 0; i < index.length; i++)  
        index[i] = -1; //        -1,  ++  0  
    for (i = 0; i < index.length - 1; i++)  
        seek(index, i); //    n-1     
    while (seek(index, index.length - 1)) { //     n   ,           
        var temp = [];  
        for (i = 0; i < index.length; i++) //           
            temp.push(arr[index[i]]);  
        show(temp);  
    }  
}  
perm(["e1", "e2", "e3", "e4"]);  
</script>  
</body>  
</html> 
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.