python标准算法实现数组全排列代码,代码来自国外网站,希望对大家有所帮助。
从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列。当m=n时所有的排列情况叫全排列。
var permArr = [],
usedChars = [];
//code from http://www.sharejs.com
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr
};
console.log(permute([5, 3, 7, 1]));
//该代码片段来自于: http://www.sharejs.com/codes/javascript/8710
代码转自:脚本分享网