Last active
February 21, 2022 15:47
-
-
Save maoxiaoke/660f545ed97fb44bc4dedee391b214ec to your computer and use it in GitHub Desktop.
__Algorithm__
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var prime = function(num){ | |
var i,j; | |
var arr = []; | |
for(i = 1; i < num; i++){ | |
for(j=2; j < i; j++){ | |
if(i%j === 0) { | |
break; | |
} | |
} | |
if(i <= j && i !=1){ | |
arr.push(i); | |
} | |
} | |
return arr; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 递归 | |
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