From 99d28fcc002dc5bb40952f01d3c975ec46b867b5 Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Fri, 15 Oct 2021 16:21:58 +0800 Subject: [PATCH 1/3] =?UTF-8?q?337.=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8D?= =?UTF-8?q?=E2=85=A2=20=E5=A2=9E=E5=8A=A0go=E7=9A=84=E8=A7=A3=E6=B3=95=20?= =?UTF-8?q?=E6=A0=91=E5=BD=A2DP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0337.打家劫舍III.md | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 949137c3..204c3815 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -368,6 +368,48 @@ class Solution: return (val1, val2) ``` +Go: + +树形DP + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func rob(root *TreeNode) int { + return max(robTree(root)) +} +func robTree(root *TreeNode)(int,int){ + if root==nil{ + return 0,0 + } + //获取左节点的偷的值与不偷的值 + left0,left1:=robTree(root.Left) + //获取右节点的偷的值与不偷的值 + right0,right1:=robTree(root.Right) + //偷 + val1:=root.Val + val1+=left1+right1 + //不偷 + val2:=0 + val2+=max(left0,left1)+max(right0,right1) + return val1,val2 +} +func max(a,b int)int{ + if a>b{ + return a + } + return b +} +``` + + + JavaScript: > 动态规划 From 77d346988b1718b34f4997e7d08be82c418edd6b Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 15 Oct 2021 20:16:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=94=B9=E8=BF=9B=200143=20=E9=87=8D?= =?UTF-8?q?=E6=8E=92=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在使用数组存储链表节点的写法中,最后再判断数组长度为偶数时时没有必要的,因为在循环体while里面,我们用的是 i<=j。 --- problems/0143.重排链表.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index c5ac9bae..a6412d2e 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -50,10 +50,6 @@ public: cur = cur->next; count++; } - if (vec.size() % 2 == 0) { // 如果是偶数,还要多处理中间的一个 - cur->next = vec[i]; - cur = cur->next; - } cur->next = nullptr; // 注意结尾 } }; @@ -249,12 +245,6 @@ public class ReorderList { cur = cur.next; count++; } - // 当是偶数的话,需要做额外处理 - if (list.size() % 2== 0){ - cur.next = list.get(l); - cur = cur.next; - } - // 注意结尾要结束一波 cur.next = null; } @@ -376,11 +366,6 @@ var reorderList = function(head, s = [], tmp) { cur = cur.next; count++; } - // 当是偶数的话,需要做额外处理 - if(list.length % 2 == 0){ - cur.next = list[l]; - cur = cur.next; - } // 注意结尾要结束一波 cur.next = null; } From 5d0d80abc978a01c2b5f6f17583f890ece8988ea Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 15 Oct 2021 21:33:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=A8=A1=E6=9D=BF=20=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20JavaScript=20=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/算法模板.md | 259 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 258 insertions(+), 1 deletion(-) diff --git a/problems/算法模板.md b/problems/算法模板.md index 216ae6b6..9fa4f1a8 100644 --- a/problems/算法模板.md +++ b/problems/算法模板.md @@ -248,7 +248,7 @@ void backtracking(参数) { ## 并查集 ```CPP - int n = 1005; // 更具题意而定 + int n = 1005; // 根据题意而定 int father[1005]; // 并查集初始化 @@ -280,6 +280,263 @@ void backtracking(参数) { (持续补充ing) ## 其他语言版本 +JavaScript: + +## 二分查找法 + +使用左闭右闭区间 + +```javascript +var search = function (nums, target) { + let left = 0, right = nums.length - 1; + // 使用左闭右闭区间 + while (left <= right) { + let mid = left + Math.floor((right - left)/2); + if (nums[mid] > target) { + right = mid - 1; // 去左面闭区间寻找 + } else if (nums[mid] < target) { + left = mid + 1; // 去右面闭区间寻找 + } else { + return mid; + } + } + return -1; +}; +``` + +使用左闭右开区间 + +```javascript +var search = function (nums, target) { + let left = 0, right = nums.length; + // 使用左闭右开区间 [left, right) + while (left < right) { + let mid = left + Math.floor((right - left)/2); + if (nums[mid] > target) { + right = mid; // 去左面闭区间寻找 + } else if (nums[mid] < target) { + left = mid + 1; // 去右面闭区间寻找 + } else { + return mid; + } + } + return -1; +}; +``` + +## KMP + +```javascript +var kmp = function (next, s) { + next[0] = -1; + let j = -1; + for(let i = 1; i < s.length; i++){ + while (j >= 0 && s[i] !== s[j + 1]) { + j = next[j]; + } + if (s[i] === s[j + 1]) { + j++; + } + next[i] = j; + } +} +``` + +## 二叉树 + +### 深度优先遍历(递归) + +二叉树节点定义: + +```javascript +function TreeNode (val, left, right) { + this.val = (val === undefined ? 0 : val); + this.left = (left === undefined ? null : left); + this.right = (right === undefined ? null : right); +} +``` + +前序遍历(中左右): + +```javascript +var preorder = function (root, list) { + if (root === null) return; + list.push(root.val); // 中 + preorder(root.left, list); // 左 + preorder(root.right, list); // 右 +} +``` + +中序遍历(左中右): + +```javascript +var inorder = function (root, list) { + if (root === null) return; + inorder(root.left, list); // 左 + list.push(root.val); // 中 + inorder(root.right, list); // 右 +} +``` + +后序遍历(左右中): + +```javascript +var postorder = function (root, list) { + if (root === null) return; + postorder(root.left, list); // 左 + postorder(root.right, list); // 右 + list.push(root.val); // 中 +} +``` + +### 深度优先遍历(迭代) + +前序遍历(中左右): + +```javascript +var preorderTraversal = function (root) { + let res = []; + if (root === null) return rs; + let stack = [root], + cur = null; + while (stack.length) { + cur = stack.pop(); + res.push(cur.val); + cur.right && stack.push(cur.right); + cur.left && stack.push(cur.left); + } + return res; +}; +``` + +中序遍历(左中右): + +```javascript +var inorderTraversal = function (root) { + let res = []; + if (root === null) return res; + let stack = []; + let cur = root; + while (stack.length !== 0 || cur !== null) { + if (cur !== null) { + stack.push(cur); + cur = cur.left; + } else { + cur = stack.pop(); + res.push(cur.val); + cur = cur.right; + } + } + return res; +}; +``` + +后序遍历(左右中): + +```javascript +var postorderTraversal = function (root) { + let res = []; + if (root === null) return res; + let stack = [root]; + let cur = null; + while (stack.length) { + cur = stack.pop(); + res.push(cur.val); + cur.left && stack.push(cur.left); + cur.right && stack.push(cur.right); + } + return res.reverse() +}; +``` + +### 广度优先遍历(队列) + +```javascript +var levelOrder = function (root) { + let res = []; + if (root === null) return res; + let queue = [root]; + while (queue.length) { + let n = queue.length; + let temp = []; + for (let i = 0; i < n; i++) { + let node = queue.shift(); + temp.push(node.val); + node.left &&queue.push(node.left); + node.right && queue.push(node.right); + } + res.push(temp); + } + return res; +}; +``` + +### 二叉树深度 + +```javascript +var getDepth = function (node) { + if (node === null) return 0; + return 1 + Math.max(getDepth(node.left), getDepth(node.right)); +} +``` + +### 二叉树节点数量 + +```javascript +var countNodes = function (root) { + if (root === null) return 0; + return 1 + countNodes(root.left) + countNodes(root.right); +} +``` + +## 回溯算法 + +```javascript +function backtracking(参数) { + if (终止条件) { + 存放结果; + return; + } + + for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) { + 处理节点; + backtracking(路径,选择列表); // 递归 + 回溯,撤销处理结果 + } +} + +``` + +## 并查集 + +```javascript + let n = 1005; // 根据题意而定 + let father = new Array(n).fill(0); + + // 并查集初始化 + function init () { + for (int i = 0; i < n; ++i) { + father[i] = i; + } + } + // 并查集里寻根的过程 + function find (u) { + return u === father[u] ? u : father[u] = find(father[u]); + } + // 将v->u 这条边加入并查集 + function join(u, v) { + u = find(u); + v = find(v); + if (u === v) return ; + father[v] = u; + } + // 判断 u 和 v是否找到同一个根 + function same(u, v) { + u = find(u); + v = find(v); + return u === v; + } +``` Java: