Skip to content

Instantly share code, notes, and snippets.

@zhanhongtao
Created October 13, 2013 03:01
Show Gist options
  • Save zhanhongtao/6957637 to your computer and use it in GitHub Desktop.
Save zhanhongtao/6957637 to your computer and use it in GitHub Desktop.
全排列算法 - JavaScript 实现
var a = [ 1, 2, 3 ];
function change( a, i, j) {
var t = a[i];
a[i] = a[j];
a[j] = t;
}
function permgen( a, n ) {
n = n == null ? a.length : n;
if ( n <= 0 ) {
return console.log( a );
}
for ( var i = 0; i < n; i++ ) {
change( a, i, n - 1);
permgen( a, n-1 );
change( a, i, n - 1);
}
}
permgen( a );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment