From 956cfda360d1b063593a0978189d9c0e7ff18eb9 Mon Sep 17 00:00:00 2001 From: mmdapl <2237221210@qq.com> Date: Fri, 28 May 2021 10:28:25 +0800 Subject: [PATCH] 5.28 update --- .DS_Store | Bin 8196 -> 8196 bytes 算法/.DS_Store | Bin 8196 -> 8196 bytes 算法/debounce.js | 24 ++++++++++ 算法/剑指/Readme.md | 16 +++---- 算法/剑指/树/FindPath.js | 46 ++++++++++++++++++ 算法/剑指/树/KthNode.js | 75 ++++++++++++++++++++++++++++++ 算法/剑指/树/Mirror.js | 47 +++++++++++++++++++ 算法/剑指/树/Print.js | 74 +++++++++++++++++++++++++++++ 算法/剑指/树/isSymmetrical.js | 45 ++++++++++++++++++ 算法/后端/getLongestPalindrome.js | 42 +++++++++++++++++ 10 files changed, 361 insertions(+), 8 deletions(-) create mode 100644 算法/debounce.js create mode 100644 算法/剑指/树/FindPath.js create mode 100644 算法/剑指/树/KthNode.js create mode 100644 算法/剑指/树/Mirror.js create mode 100644 算法/剑指/树/Print.js create mode 100644 算法/剑指/树/isSymmetrical.js create mode 100644 算法/后端/getLongestPalindrome.js diff --git a/.DS_Store b/.DS_Store index fbe501bb51fddbb57bfd3a569009caa56337e481..e5d6d6caf37a2e1bbb8d629f384a6073fdb78a6c 100644 GIT binary patch delta 161 zcmZp1XmOa}&nU4mU^hRb#AF@;ZFO#jc!p$#e1<%RG=@xubOt?!N`_p99M7Em!`*+4jrWiz|PH delta 56 zcmV-80LTA?K!iY$PXQLOP`eKS7LyDRE0b*yV6zDk&H2Hwr=94s8dAQjvUDL|Pd zhJ1*s$qE8ud@Pbc361|?z`(GXN8lrKLQcA2aB_Zb0Z;}A+%16wQ;=`f``mmNm!zEh rBp{zdbaB%yZMnma2)PuTas?U4<}F~@{8hMwc{97jHfDN+;2m}ZL7PCSS d@dLA)6l?>LfS$7l81@8_cn!0t6<-38fSY246kPxS diff --git a/算法/debounce.js b/算法/debounce.js new file mode 100644 index 0000000..8cd054e --- /dev/null +++ b/算法/debounce.js @@ -0,0 +1,24 @@ + + +function debounce(func,time){ + let timeout + return ()=>{ + clearTimeout(timeout) + timeout=setTimeout(()=>{ + func.apply(this,arguments) + // func() + },time) + } +} + +function test(){ + console.log(new Date().getTime()) +} + +const debounce_test= debounce(test,1000) +debounce_test() + +debounce_test() +// setTimeout(()=>{ +// dou(test,1000) +// },1500) \ No newline at end of file diff --git a/算法/剑指/Readme.md b/算法/剑指/Readme.md index bbe4fc1..009cae2 100644 --- a/算法/剑指/Readme.md +++ b/算法/剑指/Readme.md @@ -4,7 +4,7 @@ * @Author: 【B站&公众号】Rong姐姐好可爱 * @Date: 2021-04-27 08:39:46 * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-09 11:40:55 + * @LastEditTime: 2021-05-12 22:01:34 --> @@ -59,15 +59,15 @@ ### 树 -- 重建二叉树 -- 二叉树的下一个结点 -- 树的子结构 -- 二叉树的镜像 -- 对称的二叉树 +- [【中等】重建二叉树](./树/reConstructBinaryTree.js) +- [【中等】二叉树的下一个结点](./树/GetNext.js) +- [【较难】树的子结构](./树/HasSubtree.js) +- [【简单】二叉树的镜像](./树/Mirror.js) +- [【困难】对称的二叉树](./树/isSymmetrical.js) - 从上往下打印二叉树 - 把二叉树打印成多行 - 二叉搜索树的后续遍历序列 -- 二叉树中和为某一值的路劲 +- 【较难】二叉树中和为某一值的路径 - 二叉搜索树和双向链表 - 序列化二叉树 - 二叉查找树的第K个结点 @@ -131,7 +131,7 @@ - [【中等】二进制中1的个数](./位运算/NumberOf1.js) - [【中等】数组中只出现一次的数字]() -### 其他分类 +#### 其他分类 - [【简单】不用加减乘除做加法](./其他相关/Add.js) - [【中等】扑克牌顺子](./其他相关/IsContinuous.js) diff --git a/算法/剑指/树/FindPath.js b/算法/剑指/树/FindPath.js new file mode 100644 index 0000000..f75aa7f --- /dev/null +++ b/算法/剑指/树/FindPath.js @@ -0,0 +1,46 @@ +/* + * @Description: 【较难】二叉树中和为某一值的路径 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-05-12 22:01:00 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-05-12 22:01:45 + */ + + +/* function TreeNode(x) { + this.val = x; + this.left = null; + this.right = null; +} */ + +function FindPath (root, expectNumber) { + // write code here + let result = []; + function dfs (root, target, temp_res) { + // 处理空树 + if (root === null) { + return; + } + // 根结点,进数组 + temp_res.push(root.val); + + // 当前结点为叶子结点 + if (root.left === null && root.right === null && target === root.val) { + result.push(temp_res) + } + + // 不是叶子结点,向左向右子树递归 + dfs(root.left, target - root.val, [...temp_res]) + dfs(root.right, target - root.val, [...temp_res]) + } + dfs(root, expectNumber, []) + return result; + +} + + + +module.exports = { + FindPath: FindPath +}; \ No newline at end of file diff --git a/算法/剑指/树/KthNode.js b/算法/剑指/树/KthNode.js new file mode 100644 index 0000000..bc2d535 --- /dev/null +++ b/算法/剑指/树/KthNode.js @@ -0,0 +1,75 @@ +/* + * @Description: 【简单】二叉搜索树的第K个结点 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-05-12 19:51:41 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-05-12 20:26:21 + */ + +function ListNode (x) { + this.val = x; + this.left = null; + this.right = null; +} + +// {8,6,10,5,7,9,11},1 +const root = { + val: 8, + left: { + val: 6, + left: { + val: 5, + left: null, + right: null + }, + right: { + val: 7, + left: null, + right: null + } + }, + right: { + val: 10, + left: { + val: 9, + left: null, + right: null + }, + right: { + val: 11, + left: null, + right: { + val: 12, + left: null, + right: null + } + } + } +} + +/* function TreeNode(x) { + this.val = x; + this.left = null; + this.right = null; +} */ + +// 注意,是返回结点 +function KthNode (pRoot, k) { + // console.log(inOrder(pRoot)) + return inOrder(pRoot)[k - 1] +} + +// 中序遍历 +function inOrder (root) { + // console.log(root) + if (root === null) { + return [] + } + + return inOrder(root.left).concat([root]).concat(inOrder(root.right)) + +} +module.exports = { + KthNode: KthNode +}; \ No newline at end of file diff --git a/算法/剑指/树/Mirror.js b/算法/剑指/树/Mirror.js new file mode 100644 index 0000000..4ed6118 --- /dev/null +++ b/算法/剑指/树/Mirror.js @@ -0,0 +1,47 @@ +/* + * @Description: 【简单】二叉树的镜像 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-05-11 16:42:42 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-05-11 16:43:17 + */ + +/* + * function TreeNode(x) { + * this.val = x; + * this.left = null; + * this.right = null; + * } + */ +/** + * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 + * + * + * @param pRoot TreeNode类 + * @return TreeNode类 + */ +function Mirror (pRoot) { + // write code here + // 空树 + if (pRoot === null) { + return pRoot; + } + + // 处理根节点,交换左右子树【建议封装函数】 + [pRoot.left, pRoot.right] = [pRoot.right, pRoot.left] + + + // 左子树镜像 + Mirror(pRoot.left) + // 右子树镜像 + Mirror(pRoot.right) + + return pRoot; + +} + + +module.exports = { + Mirror: Mirror +}; \ No newline at end of file diff --git a/算法/剑指/树/Print.js b/算法/剑指/树/Print.js new file mode 100644 index 0000000..dba29c0 --- /dev/null +++ b/算法/剑指/树/Print.js @@ -0,0 +1,74 @@ +/* + * @Description: + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-05-12 21:37:07 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-05-12 21:40:15 + */ + + +/* function TreeNode(x) { + this.val = x; + this.left = null; + this.right = null; +} */ +function Print (pRoot) { + // write code here + // 层序遍历的 进阶 + + // 处理空树的情况 + if (pRoot === null) { + return []; + } + + // 临时队列 存放二叉树 + let temp_queue = [pRoot]; + // 存放结果 + let result = [] + + while (temp_queue.length > 0) { + + // 记录当前层有几个子结点 ---> 子树 + let count = temp_queue.length; + + // 记录一层的数据,先不考虑从左到右还是从右到左 + let level_arr = []; + while (count > 0) { + const currentTree = temp_queue.shift(); + + // 根结点存在 + if (currentTree !== null) { + // 直接放入根结点 + level_arr.push(currentTree.val) + // 处理左子树 + if (currentTree.left !== null) { + // 当前层的结点,进入队列 + temp_queue.push(currentTree.left) + // level_arr.push(currentTree.left.val) + } + + if (currentTree.right !== null) { + temp_queue.push(currentTree.right) + // level_arr.push(currentTree.right.val) + } + // 每次只能处理一个 + count-- + } + + } + result.push(level_arr) + } + + // 这里已经按照层序遍历输出了,后面可以对数组进行奇数|偶数处理 + // 当然,也可以在数组push的时候进行奇数|偶数计数处理 + result.map((item, index) => { + return (index + 1) % 2 === 1 ? item : item.reverse() + }) + return result; +} + + +module.exports = { + Print: Print +}; \ No newline at end of file diff --git a/算法/剑指/树/isSymmetrical.js b/算法/剑指/树/isSymmetrical.js new file mode 100644 index 0000000..a344367 --- /dev/null +++ b/算法/剑指/树/isSymmetrical.js @@ -0,0 +1,45 @@ +/* + * @Description: 【困难】对称的二叉树 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-05-11 17:52:52 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-05-11 17:53:26 + */ + +/* function TreeNode(x) { + this.val = x; + this.left = null; + this.right = null; +} */ +function isSymmetrical (pRoot) { + // write code here + if (pRoot === null) { + // 子树为空,判断子树为对称 + return true; + } + + // 不为空则判断左右子树是否对称,依据:左右子树位置互换,依旧对称 【调用一次】 + return isSameTree(pRoot.left, pRoot.right) +} + +// 判断左右子树是是否对称 +function isSameTree (leftTree, rightTree) { + if (leftTree === null && rightTree === null) { + return true; + } else if (leftTree !== null && rightTree !== null) { + // 左右子树不为空,则分别比对左右子树 + if (leftTree.val === rightTree.val) { + // 根结点相同,比对左右子树 + return isSameTree(leftTree.left, rightTree.right) && isSameTree(rightTree.left, leftTree.right); + } + + } else { + return false; + } +} + + +module.exports = { + isSymmetrical: isSymmetrical +}; \ No newline at end of file diff --git a/算法/后端/getLongestPalindrome.js b/算法/后端/getLongestPalindrome.js new file mode 100644 index 0000000..3bf36f2 --- /dev/null +++ b/算法/后端/getLongestPalindrome.js @@ -0,0 +1,42 @@ +/* + * @Description: 最长回文字符串 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-05-20 21:06:00 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-05-20 21:06:17 + */ + + +/** + * 求给定字符的最大回文字符串 + * @param {string} str 字符串 + * @param {int} len 给定字符串的长度 + */ +function getLongestPalindrome(str,len){ + + + // 直接暴力 + + // 最大计数 + let max=0; + for(let i=0;i