From dfe98e3766ed8b2a84a718d09c5bef1c415379ac Mon Sep 17 00:00:00 2001 From: syuxin2019 <519802856@qq.com> Date: Fri, 18 Nov 2022 19:12:21 +0800 Subject: [PATCH 01/12] modified 707.md --- problems/0707.设计链表.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 1264983b..5393c038 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -106,11 +106,10 @@ public: // 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。 // 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点 // 如果index大于链表的长度,则返回空 - // 如果index小于0,则置为0,作为链表的新头节点。 + // 如果index小于0,则在头部插入节点 void addAtIndex(int index, int val) { - if (index > _size || index < 0) { - return; - } + if(index > _size) return; + if(index < 0) index = 0; LinkedNode* newNode = new LinkedNode(val); LinkedNode* cur = _dummyHead; while(index--) { From 944ce27a28b6582e71f3626c39708e84ec4a0c34 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 19 Nov 2022 00:07:04 +0800 Subject: [PATCH 02/12] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index e35a5214..dc576c60 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -617,6 +617,38 @@ func _binaryTreePaths3(_ root: TreeNode, res: inout [String], paths: inout [Int] } ``` +### Rust + +> 100.相同的树 + +```rsut +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn is_same_tree( + p: Option>>, + q: Option>>, + ) -> bool { + match (p, q) { + (None, None) => true, + (None, Some(_)) => false, + (Some(_), None) => false, + (Some(n1), Some(n2)) => { + if n1.borrow().val == n2.borrow().val { + let right = + Self::is_same_tree(n1.borrow().left.clone(), n2.borrow().left.clone()); + let left = + Self::is_same_tree(n1.borrow().right.clone(), n2.borrow().right.clone()); + right && left + } else { + false + } + } + } + } +} +``` +

From 4bae1e84baffb99c60f073eeca515c9c4a14697b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 19 Nov 2022 01:39:14 +0800 Subject: [PATCH 03/12] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index dc576c60..9bf7405d 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -649,6 +649,43 @@ impl Solution { } ``` +> 257.二叉树的不同路径 + +```rust +// 递归 +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn binary_tree_paths(root: Option>>) -> Vec { + let mut res = vec![]; + let mut path = vec![]; + Self::recur(&root, &mut path, &mut res); + res + } + + pub fn recur( + root: &Option>>, + path: &mut Vec, + res: &mut Vec, + ) { + let node = root.as_ref().unwrap().borrow(); + path.push(node.val.to_string()); + if node.left.is_none() && node.right.is_none() { + res.push(path.join("->")); + return; + } + if node.left.is_some() { + Self::recur(&node.left, path, res); + path.pop(); //回溯 + } + if node.right.is_some() { + Self::recur(&node.right, path, res); + path.pop(); //回溯 + } + } +} +``` +

From 65857d15333bfe5ecf9a7f56fb0abb8a4c30ebe2 Mon Sep 17 00:00:00 2001 From: Zeeland <287017217@qq.com> Date: Sat, 19 Nov 2022 09:50:57 +0800 Subject: [PATCH 04/12] =?UTF-8?q?update=200695.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0695.岛屿的最大面积.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 414efe0b..76740ef1 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -197,7 +197,6 @@ class Solution: if not visited[i][j] and grid[i][j] == 1: # 每一个新岛屿 self.count = 0 - print(f'{self.count}') self.bfs(grid, visited, i, j) result = max(result, self.count) From 15379361aa9b1c1c062870d9e4790ca24ae51a1b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 16:11:25 +0800 Subject: [PATCH 05/12] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 6b6fe729..3118b7c0 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -575,6 +575,31 @@ object Solution { } ``` +### Rust + +**递归** + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn sum_of_left_leaves(root: Option>>) -> i32 { + let mut res = 0; + if let Some(node) = root { + if let Some(left) = &node.borrow().left { + if left.borrow().right.is_none() && left.borrow().right.is_none() { + res += left.borrow().val; + } + } + res + Self::sum_of_left_leaves(node.borrow().left.clone()) + + Self::sum_of_left_leaves(node.borrow().right.clone()) + } else { + 0 + } + } +} +``` +

From 59d17ea08d37ab5e85cae19c472686fa0a4d4a69 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 16:39:42 +0800 Subject: [PATCH 06/12] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 3118b7c0..f5b9ca39 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -600,6 +600,33 @@ impl Solution { } ``` +**迭代:** + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn sum_of_left_leaves(root: Option>>) -> i32 { + let mut res = 0; + let mut stack = vec![root]; + while !stack.is_empty() { + if let Some(node) = stack.pop().unwrap() { + if let Some(left) = &node.borrow().left { + if left.borrow().left.is_none() && left.borrow().right.is_none() { + res += left.borrow().val; + } + stack.push(Some(left.to_owned())); + } + if let Some(right) = &node.borrow().right { + stack.push(Some(right.to_owned())); + } + } + } + res + } +} +``` +

From 145114872d6d43e6f3e938891249bf52c7aea8c2 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 16:58:25 +0800 Subject: [PATCH 07/12] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index fd6e5d95..cfb4a6c4 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -587,6 +587,40 @@ object Solution { } ``` +### rust + +**层序遍历** + +```rust +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn find_bottom_left_value(root: Option>>) -> i32 { + let mut queue = VecDeque::new(); + let mut res = 0; + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + for i in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + if i == 1 { + res = node.borrow().val; + } + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + } + res + } +} +``` +

From 25e055ec7343a7ff79303062fbbf32ec22b9e02e Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 16:59:01 +0800 Subject: [PATCH 08/12] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index cfb4a6c4..70be7051 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -605,7 +605,7 @@ impl Solution { while !queue.is_empty() { for i in 0..queue.len() { let node = queue.pop_front().unwrap().unwrap(); - if i == 1 { + if i == 0 { res = node.borrow().val; } if node.borrow().left.is_some() { From 032527d25ad573ab3e7c3c715d2f6d3d66f0968f Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:43:25 +0800 Subject: [PATCH 09/12] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 70be7051..622a11b5 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -621,6 +621,43 @@ impl Solution { } ``` +**递归** + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + //*递归*/ + pub fn find_bottom_left_value(root: Option>>) -> i32 { + let mut res = 0; + let mut max_depth = i32::MIN; + Self::traversal(root, 0, &mut max_depth, &mut res); + res + } + fn traversal( + root: Option>>, + depth: i32, + max_depth: &mut i32, + res: &mut i32, + ) { + let node = root.unwrap(); + if node.borrow().left.is_none() && node.borrow().right.is_none() { + if depth > *max_depth { + *max_depth = depth; + *res = node.borrow().val; + } + return; + } + if node.borrow().left.is_some() { + Self::traversal(node.borrow().left.clone(), depth + 1, max_depth, res); + } + if node.borrow().right.is_some() { + Self::traversal(node.borrow().right.clone(), depth + 1, max_depth, res); + } + } +} +``` +

From d3e35b3ae961d14dbcb23a9658d3163ca50dd284 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 18:02:32 +0800 Subject: [PATCH 10/12] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index d50f23f9..482787bf 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -1213,6 +1213,28 @@ object Solution { } ``` +## rust + +### 112.路径总和.md + +```rust +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + pub fn has_path_sum(root: Option>>, target_sum: i32) -> bool { + if root.is_none() { + return false; + } + let node = root.unwrap(); + if node.borrow().left.is_none() && node.borrow().right.is_none() { + return node.borrow().val == target_sum; + } + return Self::has_path_sum(node.borrow().left.clone(), target_sum - node.borrow().val) + || Self::has_path_sum(node.borrow().right.clone(), target_sum - node.borrow().val); + } +} +``` +

From 47b7cf3cae785ec43be3e08dc8db8143d3f9aaca Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 22:06:39 +0800 Subject: [PATCH 11/12] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 482787bf..f717f6b8 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -1217,6 +1217,8 @@ object Solution { ### 112.路径总和.md +递归: + ```rust use std::rc::Rc; use std::cell::RefCell; @@ -1235,6 +1237,38 @@ impl Solution { } ``` +迭代: + +```rust +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + pub fn has_path_sum(root: Option>>, target_sum: i32) -> bool { + let mut stack = vec![]; + if let Some(node) = root { + stack.push((node.borrow().val, node.to_owned())); + } + while !stack.is_empty() { + let (value, node) = stack.pop().unwrap(); + if node.borrow().left.is_none() && node.borrow().right.is_none() && value == target_sum + { + return true; + } + if node.borrow().left.is_some() { + if let Some(r) = node.borrow().left.as_ref() { + stack.push((r.borrow().val + value, r.to_owned())); + } + } + if node.borrow().right.is_some() { + if let Some(r) = node.borrow().right.as_ref() { + stack.push((r.borrow().val + value, r.to_owned())); + } + } + } + false + } +``` +

From 1fef4a77be4ceefde5406a774706c00de77917cc Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 21 Nov 2022 22:31:55 +0800 Subject: [PATCH 12/12] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index f717f6b8..effe7649 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -1269,6 +1269,55 @@ impl Solution { } ``` +### 113.路径总和-ii + +```rust +impl Solution { + pub fn path_sum(root: Option>>, target_sum: i32) -> Vec> { + let mut res = vec![]; + let mut route = vec![]; + if root.is_none() { + return res; + } else { + route.push(root.as_ref().unwrap().borrow().val); + } + Self::recur( + &root, + target_sum - root.as_ref().unwrap().borrow().val, + &mut res, + &mut route, + ); + res + } + + pub fn recur( + root: &Option>>, + sum: i32, + res: &mut Vec>, + route: &mut Vec, + ) { + let node = root.as_ref().unwrap().borrow(); + if node.left.is_none() && node.right.is_none() && sum == 0 { + res.push(route.to_vec()); + return; + } + if node.left.is_some() { + let left = node.left.as_ref().unwrap(); + route.push(left.borrow().val); + Self::recur(&node.left, sum - left.borrow().val, res, route); + route.pop(); + } + if node.right.is_some() { + let right = node.right.as_ref().unwrap(); + route.push(right.borrow().val); + Self::recur(&node.right, sum - right.borrow().val, res, route); + route.pop(); + } + } +} + +``` +