1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-04-14 18:30:30 +08:00

5.28 update

This commit is contained in:
mmdapl
2021-05-28 10:28:25 +08:00
parent 06fa198f98
commit 956cfda360
10 changed files with 361 additions and 8 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
算法/.DS_Store vendored

Binary file not shown.

24
算法/debounce.js Normal file
View File

@@ -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)

View File

@@ -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)

View File

@@ -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
};

View File

@@ -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
};

View File

@@ -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
};

View File

@@ -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
};

View File

@@ -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
};

View File

@@ -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<len;i++){
for(let j=i+1;j<len;j++){
let current_str=str.slice(i,j);
let reverse_srt=str.slice(i,j).split('').reverse().join()
if(current_str===reverse_srt){
// 可以
max=Math.max(max,j-i+1)
}else{
// 不可能是回文
break;
}
}
}
return max;
}