数组内字符的全排列算法

来源:互联网 发布:无翼鸟app软件下载 编辑:程序博客网 时间:2024/06/05 03:14

原文链接:JS实现的数组全排列输出算法

function permute(input) {  var permArr = [],  usedChars = [];  function main(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());      }      main(input);      input.splice(i, 0, ch);      usedChars.pop();    }    return permArr  }  return main(input);};console.log(permute([5, 3, 7, 1]));
0 0
原创粉丝点击