Skip to content

Instantly share code, notes, and snippets.

@maoxiaoke
Last active February 21, 2022 15:47
Show Gist options
  • Select an option

  • Save maoxiaoke/660f545ed97fb44bc4dedee391b214ec to your computer and use it in GitHub Desktop.

Select an option

Save maoxiaoke/660f545ed97fb44bc4dedee391b214ec to your computer and use it in GitHub Desktop.
__Algorithm__
/**
* Heap's algorithm
*/
function heapPermutation(arr,size,n){
var swap = function (index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
if (size === 1){
console.log(arr);
return;
}
for (let i = 0; i < size; i++){
heapPermutation(arr, size -1,n);
swap(size % 2 ? 0 : i, size - 1);
}
}
var arr = ['a','b','c','d'];
heapPermutation(arr,arr.length,arr.length);
// 递归
function gcd(m ,n){
if(n) return gcd(n,m%n);
return m;
}
/**
* Heap's algorithm
*/
function heapPermutation(arr,size,n){
var swap = function (index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
if (size === 1){
console.log(arr);
return;
}
for (let i = 0; i < size; i++){
heapPermutation(arr, size -1,n);
swap(size % 2 ? 0 : i, size - 1);
}
}
var arr = ['a','b','c','d'];
heapPermutation(arr,arr.length,arr.length);
// 递归
function gcd(m ,n){
if(n) return gcd(n,m%n);
return m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment