From f9bddcabeb72f3696773091744b2d7d53387e895 Mon Sep 17 00:00:00 2001 From: HaoYu Dong <73568310+DongHY1@users.noreply.github.com> Date: Mon, 21 Nov 2022 22:21:39 +0800 Subject: [PATCH 1/5] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixing description issues --- problems/0101.对称二叉树.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 30ca3d77..f3d9fa1d 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -21,7 +21,7 @@ 对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。 -那么如果比较呢? +那么如何比较呢? 比较的是两个子树的里侧和外侧的元素是否相等。如图所示: From f071df11f81f0090e611d8700d9868f38ecde55c Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 22 Nov 2022 00:01:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 17ba561d..0353ad62 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1136,6 +1136,34 @@ object Solution { } ``` +## rust + +105 从前序与中序遍历序列构造二叉树 + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn build_tree( + mut preorder: Vec, + mut inorder: Vec, + ) -> Option>> { + if preorder.is_empty() { + return None; + } + let root = preorder[0]; + let index = inorder.iter().position(|&x| x == root).unwrap(); + let mut root = TreeNode::new(root); + root.left = Self::build_tree( + preorder.splice(1..index + 1, []).collect(), + inorder.splice(0..index, []).collect(), + ); + root.right = Self::build_tree(preorder.split_off(1), inorder.split_off(1)); + Some(Rc::new(RefCell::new(root))) + } +} +``` +

From 3762f1a3e3e4f4f5ec1e3efc4d6ec9cf77079244 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 22 Nov 2022 00:18:08 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 0353ad62..cf5f11d0 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1138,6 +1138,30 @@ object Solution { ## rust +106 从中序与后序遍历序列构造二叉树 + +```rust +impl Solution { + pub fn build_tree( + mut inorder: Vec, + mut postorder: Vec, + ) -> Option>> { + if inorder.is_empty() { + return None; + } + let root = postorder.pop().unwrap(); + let index = inorder.iter().position(|&x| x == root).unwrap(); + let mut root = TreeNode::new(root); + root.left = Self::build_tree( + inorder.splice(0..index, []).collect(), + postorder.splice(0..index, []).collect(), + ); + root.right = Self::build_tree(inorder.split_off(1), postorder); + Some(Rc::new(RefCell::new(root))) + } +} +``` + 105 从前序与中序遍历序列构造二叉树 ```rust From e3a6f81303d43bff02841d6440c93eb159a2bbd7 Mon Sep 17 00:00:00 2001 From: symdunstaz <103296105+symdunstaz@users.noreply.github.com> Date: Tue, 22 Nov 2022 16:56:31 +0800 Subject: [PATCH 4/5] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了 背包理论基础01背包-1 中的 java版本的代码 --- problems/背包理论基础01背包-1.md | 71 ++++++++++++++++-------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index ee19e53d..8f4ca2d4 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -271,39 +271,64 @@ int main() { ### java ```java +public class BagProblem { public static void main(String[] args) { - int[] weight = {1, 3, 4}; - int[] value = {15, 20, 30}; - int bagsize = 4; - testweightbagproblem(weight, value, bagsize); + int[] weight = {1,3,4}; + int[] value = {15,20,30}; + int bagSize = 4; + testWeightBagProblem(weight,value,bagSize); } - public static void testweightbagproblem(int[] weight, int[] value, int bagsize){ - int wlen = weight.length, value0 = 0; - //定义dp数组:dp[i][j]表示背包容量为j时,前i个物品能获得的最大价值 - int[][] dp = new int[wlen + 1][bagsize + 1]; - //初始化:背包容量为0时,能获得的价值都为0 - for (int i = 0; i <= wlen; i++){ - dp[i][0] = value0; + /** + * 动态规划获得结果 + * @param weight 物品的重量 + * @param value 物品的价值 + * @param bagSize 背包的容量 + */ + public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ + + // 创建dp数组 + int goods = weight.length; // 获取物品的数量 + int[][] dp = new int[goods][bagSize + 1]; + + // 初始化dp数组 + // 创建数组后,其中默认的值就是0 + for (int j = weight[0]; j <= bagSize; j++) { + dp[0][j] = value[0]; } - //遍历顺序:先遍历物品,再遍历背包容量 - for (int i = 1; i <= wlen; i++){ - for (int j = 1; j <= bagsize; j++){ - if (j < weight[i - 1]){ - dp[i][j] = dp[i - 1][j]; - }else{ - dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]); + + // 填充dp数组 + for (int i = 1; i < weight.length; i++) { + for (int j = 1; j <= bagSize; j++) { + if (j < weight[i]) { + /** + * 当前背包的容量都没有当前物品i大的时候,是不放物品i的 + * 那么前i-1个物品能放下的最大价值就是当前情况的最大价值 + */ + dp[i][j] = dp[i-1][j]; + } else { + /** + * 当前背包的容量可以放下物品i + * 那么此时分两种情况: + * 1、不放物品i + * 2、放物品i + * 比较这两种情况下,哪种背包中物品的最大价值最大 + */ + dp[i][j] = Math.max(dp[i-1][j] , dp[i-1][j-weight[i]] + value[i]); } } } - //打印dp数组 - for (int i = 0; i <= wlen; i++){ - for (int j = 0; j <= bagsize; j++){ - System.out.print(dp[i][j] + " "); + + // 打印dp数组 + for (int i = 0; i < goods; i++) { + for (int j = 0; j <= bagSize; j++) { + System.out.print(dp[i][j] + "\t"); } - System.out.print("\n"); + System.out.println("\n"); } } +} + ``` ### python From 183a62a9237e0b2e3710985610421984400911e7 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Wed, 23 Nov 2022 01:57:45 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index cf5f11d0..e5cbeda3 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1141,22 +1141,19 @@ object Solution { 106 从中序与后序遍历序列构造二叉树 ```rust +use std::cell::RefCell; +use std::rc::Rc; impl Solution { - pub fn build_tree( - mut inorder: Vec, - mut postorder: Vec, - ) -> Option>> { + pub fn build_tree(inorder: Vec, postorder: Vec) -> Option>> { if inorder.is_empty() { return None; } + let mut postorder = postorder; let root = postorder.pop().unwrap(); let index = inorder.iter().position(|&x| x == root).unwrap(); let mut root = TreeNode::new(root); - root.left = Self::build_tree( - inorder.splice(0..index, []).collect(), - postorder.splice(0..index, []).collect(), - ); - root.right = Self::build_tree(inorder.split_off(1), postorder); + root.left = Self::build_tree(inorder[..index].to_vec(), postorder[..index].to_vec()); + root.right = Self::build_tree(inorder[index + 1..].to_vec(), postorder[index..].to_vec()); Some(Rc::new(RefCell::new(root))) } } @@ -1168,21 +1165,18 @@ impl Solution { use std::cell::RefCell; use std::rc::Rc; impl Solution { - pub fn build_tree( - mut preorder: Vec, - mut inorder: Vec, - ) -> Option>> { + pub fn build_tree(preorder: Vec, inorder: Vec) -> Option>> { if preorder.is_empty() { return None; } let root = preorder[0]; let index = inorder.iter().position(|&x| x == root).unwrap(); let mut root = TreeNode::new(root); - root.left = Self::build_tree( - preorder.splice(1..index + 1, []).collect(), - inorder.splice(0..index, []).collect(), + root.left = Self::build_tree(preorder[1..index + 1].to_vec(), inorder[0..index].to_vec()); + root.right = Self::build_tree( + preorder[index + 1..].to_vec(), + inorder[index + 1..].to_vec(), ); - root.right = Self::build_tree(preorder.split_off(1), inorder.split_off(1)); Some(Rc::new(RefCell::new(root))) } }