首页 > 代码库 > 全排列算法 --javascript 实现

全排列算法 --javascript 实现

var Ann = function a(arr){

if(arr.length == 1){return arr;}

var rr = new Array();
for(var i = 0; i<arr.length;i++){

//get a copy
var ar = new Array();
for(var j = 0; j < arr.length;j++){ar[j] = arr[j];}

//assume i
var current = ar[i];
ar.splice(i,1);

var childRet = a(ar);

for(var k = 0 ;k < childRet.length;k++){
rr.push(current + "," + childRet[k]);
}

}

return rr;
}

var ret = Ann(new Array(1,2,3,4));
console.log(ret);

全排列算法 --javascript 实现