function swap(index1, index2) {
tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
function generate(int) {
if (int === 1) {
// Make sure to join the characters as we create the permutation arrays
order.push(arr.join(''));
} else {
for (var i = 0; i != int; ++i) {
generate(int - 1);
swap(int % 2 ? 0 : i, int - 1);
}
}
}
var order = [];
arr=['a', 'b', 'c', 'd'];
generate(arr.length);
console.log(order);
发布评论
评论(2)
http://blog.csdn.net/LaoJiu_/...
不过是C++写的,话说写算法干嘛用js。。。。真的是。。至少会个c啊
Heap's Algorithm