├── README.md ├── stairs.js ├── flat.js ├── bubke.js ├── fib.js ├── change1.js ├── hash.js ├── stack.js ├── tree.html ├── change.js ├── quick.html └── quick.js /README.md: -------------------------------------------------------------------------------- 1 | # algorithm-lesson 2 | 算法演示代码 3 | -------------------------------------------------------------------------------- /stairs.js: -------------------------------------------------------------------------------- 1 | // 10个楼梯 2 | // 你一次可以爬一个,也可以爬两个 3 | 4 | // 第10级,又两个方式到达 5 | // 1. 8级爬俩 6 | // 2 9级 爬一个 -------------------------------------------------------------------------------- /flat.js: -------------------------------------------------------------------------------- 1 | // 数组打平 2 | 3 | 4 | let arr = [1,2,3,[4,5,[6,7]],[8,9]] 5 | Array.prototype.flat = function(){ 6 | let arr = [] 7 | this.forEach((item)=>{ 8 | if(Array.isArray(item)){ 9 | arr=arr.concat(item.flat()) 10 | }else{ 11 | arr.push(item) 12 | } 13 | }) 14 | return arr 15 | } 16 | console.log(arr.flat()) 17 | -------------------------------------------------------------------------------- /bubke.js: -------------------------------------------------------------------------------- 1 | let arr = [11,4,3,2,1,9,6,0] 2 | // 冒泡 3 | function bubleSort(arr){ 4 | const len = arr.length 5 | for(let o=len;o>=2;o--){ 6 | // 主要是遍历次数 7 | for(let i=0;i<=o-1;i++){ 8 | if(arr[i]>arr[i+1]){ 9 | [ arr[i], arr[i+1] ]= [ arr[i+1], arr[i] ] 10 | } 11 | } 12 | } 13 | return arr 14 | } 15 | 16 | console.log(bubleSort(arr)) 17 | // 冒泡排序稳定 -------------------------------------------------------------------------------- /fib.js: -------------------------------------------------------------------------------- 1 | // [1,1,2,3,5,8,13] 2 | // 第一个版本 暴力递归 3 | function fib(n){ 4 | if(n==1 || n==2){ 5 | return 1 6 | } 7 | return fib(n-1) + fib(n-2) 8 | } 9 | 10 | // 中间存储 缓存版递归 11 | 12 | function fib1(n){ 13 | let cache = [] 14 | return helper(cache,n) 15 | } 16 | function helper(cache,n){ 17 | if(n==1 || n==2){ 18 | return 1 19 | } 20 | if(cache[n]) return cache[n] 21 | cache[n] = helper(cache,n-1) + helper(cache,n-2) 22 | return cache[n] 23 | } 24 | 25 | // 自底向上 26 | function fib2(n){ 27 | let dp = [] 28 | dp[1] = dp[2] = 1 29 | for(let i=3;i<=n;i++){ 30 | dp[i] = dp[i-1]+dp[i-2] 31 | } 32 | return dp[n] 33 | } 34 | 35 | console.time('fib') 36 | console.log(fib2(145)) 37 | console.timeEnd('fib') 38 | // 最简单的dp 39 | -------------------------------------------------------------------------------- /change1.js: -------------------------------------------------------------------------------- 1 | 2 | // 贪心 3 | class Change { 4 | constructor(changeType){ 5 | this.changeType = changeType.sort((r1, r2) => r2 - r1) 6 | 7 | } 8 | makeChange(amount) { 9 | const arr = [] 10 | for (let i = 0; i < this.changeType.length; i++) { 11 | while (amount - this.changeType[i] >= 0) { 12 | arr.push(this.changeType[i]) 13 | amount = amount - this.changeType[i] 14 | } 15 | } 16 | return arr 17 | } 18 | } 19 | 20 | const change = new Change([1, 5, 10, 20,50,100]) 21 | 22 | console.log(change.makeChange(36)) 23 | console.log(change.makeChange(136)) 24 | 25 | console.log('-'.repeat(100)) 26 | const change1 = new Change([1, 3, 4]) 27 | 28 | console.log(change1.makeChange(6)) // 其实33最好 -------------------------------------------------------------------------------- /hash.js: -------------------------------------------------------------------------------- 1 | class HashTable{ 2 | constructor(){ 3 | this.items = [] 4 | } 5 | get(key){ 6 | const hash = this.keyToHash(key) 7 | return this.items[hash] 8 | } 9 | set(key,value){ 10 | const hash = this.keyToHash(key) 11 | this.items[hash] = value 12 | } 13 | remove(key){ 14 | const hash = this.keyToHash(key) 15 | delete this.items[hash] 16 | } 17 | keyToHash(key){ 18 | // 把字符串key,变成数字 19 | let hash = 0 20 | for(let i=0;i{ 15 | // {} 16 | // } 17 | 18 | // 匹配括号语法 19 | 20 | // [1,2,3] 21 | 22 | // ({(())({})}) 匹配的 23 | // ({(()){})}) 不匹配了 24 | // 判定给的符号,是不是括号匹配正确 25 | 26 | // ({(())({})}) 27 | // [(,{,(,] (, 28 | function isBlance(symbol){ 29 | const stack = new Stack() 30 | const left = '({' 31 | const right =')}' 32 | 33 | let popValue 34 | let blance = true 35 | 36 | for(let i=0;i{return ()=>{}})}")) 58 | 59 | // html规范校验 (jsx解析) 60 | // 表达式计算 -------------------------------------------------------------------------------- /tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 |
12 |
哈喽
13 |

开课吧

14 |
15 | 123 16 | 17 |
18 |
    19 |
  • 123
  • 20 |
21 |
22 | 58 | 59 | -------------------------------------------------------------------------------- /change.js: -------------------------------------------------------------------------------- 1 | // 找零 2 | 3 | // 我们的零钱 1 5 10 20 50 100 4 | // 怎么找钱最合适 5 | 6 | class Change{ 7 | constructor(changeType){ 8 | this.changeType = changeType 9 | this.cache = {} 10 | } 11 | makeChange(amount){ 12 | // 最少张数的一个数组 13 | let min = [] 14 | if(!amount){ 15 | return [] 16 | } 17 | if(this.cache[amount]){ 18 | return this.cache[amount] 19 | } 20 | for(let i=0;i=0){ 25 | // 只要还得继续找钱 26 | // 下一步找钱的数组 27 | newMin = this.makeChange(leftAmount) 28 | } 29 | if(leftAmount>=0 && (newMin.length 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | 64 | 65 | -------------------------------------------------------------------------------- /quick.js: -------------------------------------------------------------------------------- 1 | // 先来一版好懂的 但是空间复杂度差一些 2 | let arr = [8,11,4,3,2,1,9,6,0] 3 | function quickSort(arr){ 4 | // 终止条件 5 | if(arr.length<=1){ 6 | return arr 7 | } 8 | let left = [] 9 | let right = [] 10 | let flag = arr.shift() 11 | for(let i=0;i=high){ 32 | return 33 | } 34 | let left = low 35 | let right = high 36 | let flag = arr[left] 37 | while(left=arr[left]){ 45 | left ++ 46 | } 47 | arr[right] = arr[left] 48 | } 49 | arr[left] = flag 50 | // quickSort1(arr,low,left-1) 51 | // quickSort1(arr,left+1,high) 52 | return arr 53 | 54 | } 55 | console.log([8,11,4,3,2,1,9,6,0]) 56 | console.log(quickSort1(arr)) 57 | 58 | 59 | // 为啥要取第一个 60 | // 其实随机的 只是为了发方便 61 | // 快速排序的复杂度是多少 (时间) 62 | // n*log n 63 | // 快速排序啥时候复杂度最差,如果一个数字已经排好序的 64 | // 复杂度是O(n^2) 取第1个的不好 65 | // 1. 随机取索引 66 | // 2. 快排之前,先做一下乱序 67 | 68 | // [3,2,4,6,2,1] 69 | // [4,2,3,6,2,1] --------------------------------------------------------------------------------