From 67a84f74c134a519c4681e00f89fe9f0663c66d3 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:17:20 +0800 Subject: [PATCH 01/16] =?UTF-8?q?update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1ed0c5d6..e6a58be3 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -622,6 +622,48 @@ class MyQueue() { } ``` +rust: + +```rust +struct MyQueue { + stack_in: Vec, + stack_out: Vec, +} +impl MyQueue { + + fn new() -> Self { + MyQueue { + stack_in: Vec::new(), + stack_out: Vec::new(), + } + + } + + fn push(&mut self, x: i32) { + self.stack_in.push(x); + } + + fn pop(&mut self) -> i32 { + if self.stack_out.is_empty(){ + while !self.stack_in.is_empty() { + self.stack_out.push(self.stack_in.pop().unwrap()); + } + } + self.stack_out.pop().unwrap() + } + + fn peek(&mut self) -> i32 { + let res = self.pop(); + self.stack_out.push(res); + res + } + + fn empty(&self) -> bool { + self.stack_in.is_empty() && self.stack_out.is_empty() + } +} +``` +

From 7950be58a29b0757189d3afdb25ccf45e1bbf2a6 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:18:34 +0800 Subject: [PATCH 02/16] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index e6a58be3..5858f9e0 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -630,7 +630,6 @@ struct MyQueue { stack_out: Vec, } impl MyQueue { - fn new() -> Self { MyQueue { stack_in: Vec::new(), From 9769fefe0f4305eabbf275d2db61e8cdd2871c31 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:47:41 +0800 Subject: [PATCH 03/16] =?UTF-8?q?update=200225.=E7=94=A8=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 9aa8d8a1..2f015272 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -1018,7 +1018,7 @@ class MyStack { } } ``` -> 单对列 +> 单队列 ```php class MyStack { public $queue; @@ -1051,6 +1051,44 @@ class MyStack { } } ``` + +> rust:单队列 + +```rust +struct MyStack { + queue: Vec, +} + +impl MyStack { + fn new() -> Self { + MyStack { queue: vec![] } + } + + fn push(&mut self, x: i32) { + self.queue.push(x); + } + + fn pop(&mut self) -> i32 { + let len = self.queue.len() - 1; + for _ in 0..len { + let tmp = self.queue.remove(0); + self.queue.push(tmp); + } + self.queue.remove(0) + } + + fn top(&mut self) -> i32 { + let res = self.pop(); + self.queue.push(res); + res + } + + fn empty(&self) -> bool { + self.queue.is_empty() + } +} +``` +

From 2df651a1358066246a72f3697dd04407628261d8 Mon Sep 17 00:00:00 2001 From: asiL-tcefreP <1626680964@qq.com> Date: Sat, 29 Oct 2022 13:54:57 -0400 Subject: [PATCH 04/16] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2.md=20(Ja?= =?UTF-8?q?va=E7=89=88=E6=9C=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0084.柱状图中最大的矩形.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index 220b096e..bcb0915e 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -206,7 +206,7 @@ class Solution { public int largestRectangleArea(int[] heights) { int length = heights.length; int[] minLeftIndex = new int [length]; - int[] maxRigthIndex = new int [length]; + int[] minRightIndex = new int [length]; // 记录左边第一个小于该柱子的下标 minLeftIndex[0] = -1 ; for (int i = 1; i < length; i++) { @@ -215,17 +215,17 @@ class Solution { while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t]; minLeftIndex[i] = t; } - // 记录每个柱子 右边第一个小于该柱子的下标 - maxRigthIndex[length - 1] = length; + // 记录每个柱子右边第一个小于该柱子的下标 + minRightIndex[length - 1] = length; for (int i = length - 2; i >= 0; i--) { int t = i + 1; - while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t]; - maxRigthIndex[i] = t; + while(t < length && heights[t] >= heights[i]) t = minRightIndex[t]; + minRightIndex[i] = t; } // 求和 int result = 0; for (int i = 0; i < length; i++) { - int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1); + int sum = heights[i] * (minRightIndex[i] - minLeftIndex[i] - 1); result = Math.max(sum, result); } return result; From 2e5466bdce0a3d884a1a9084da74732a80fe806d Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:41:54 +0800 Subject: [PATCH 05/16] =?UTF-8?q?update=200020.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 3d986a82..69713c62 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -493,6 +493,33 @@ object Solution { } ``` +rust: + +```rust +impl Solution { + pub fn is_valid(s: String) -> bool { + if s.len() % 2 == 1 { + return false; + } + let mut stack = vec![]; + let mut chars: Vec = s.chars().collect(); + while let Some(s) = chars.pop() { + match s { + ')' => stack.push('('), + ']' => stack.push('['), + '}' => stack.push('{'), + _ => { + if stack.is_empty() || stack.pop().unwrap() != s { + return false; + } + } + } + } + stack.is_empty() + } +} +``` +

From 309b49cf47699554a928354e2621575ec524db2f Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:59:59 +0800 Subject: [PATCH 06/16] =?UTF-8?q?update=201047.=E5=88=A0=E9=99=A4=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89=E7=9B=B8?= =?UTF-8?q?=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 849b5e79..ad729298 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -447,6 +447,25 @@ object Solution { } ``` +rust: + +```rust +impl Solution { + pub fn remove_duplicates(s: String) -> String { + let mut stack = vec![]; + let mut chars: Vec = s.chars().collect(); + while let Some(c) = chars.pop() { + if stack.is_empty() || stack[stack.len() - 1] != c { + stack.push(c); + } else { + stack.pop(); + } + } + stack.into_iter().rev().collect() + } +} +``` +

From 3db9b494f9c433b2c8e9c350200a3ca877788865 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 31 Oct 2022 11:53:08 +0800 Subject: [PATCH 07/16] =?UTF-8?q?update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0150.逆波兰表达式求值.md | 39 ++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 108c654b..78dfae3e 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -89,12 +89,8 @@ C++代码如下: class Solution { public: int evalRPN(vector& tokens) { -<<<<<<< HEAD - stack st; -======= // 力扣修改了后台测试数据,需要用longlong stack st; ->>>>>>> 28f3b52a82e3cc650290fb02030a53900e122f43 for (int i = 0; i < tokens.size(); i++) { if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { long long num1 = st.top(); @@ -424,6 +420,41 @@ object Solution { } ``` + +rust: + +```rust +impl Solution { + pub fn eval_rpn(tokens: Vec) -> i32 { + let mut stack = vec![]; + for token in tokens.into_iter() { + match token.as_str() { + "+" => { + let a = stack.pop().unwrap(); + *stack.last_mut().unwrap() += a; + } + "-" => { + let a = stack.pop().unwrap(); + *stack.last_mut().unwrap() -= a; + } + "*" => { + let a = stack.pop().unwrap(); + *stack.last_mut().unwrap() *= a; + } + "/" => { + let a = stack.pop().unwrap(); + *stack.last_mut().unwrap() /= a; + } + _ => { + stack.push(token.parse::().unwrap()); + } + } + } + stack.pop().unwrap() + } +} +``` +

From 5b3b774083e5e204bd777652d6eb2452d83f06c4 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 31 Oct 2022 13:10:25 +0800 Subject: [PATCH 08/16] =?UTF-8?q?update=200239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 2d556d8b..40da879c 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -802,6 +802,35 @@ class myDequeue{ } ``` +rust: + +```rust +impl Solution { + pub fn max_sliding_window(nums: Vec, k: i32) -> Vec { + let mut res = vec![]; + let mut queue = VecDeque::with_capacity(k as usize); + for (i, &v) in nums.iter().enumerate() { + // 如果队列长度超过 k,那么需要移除队首过期元素 + if i - queue.front().unwrap_or(&0) == k as usize { + queue.pop_front(); + } + while let Some(&index) = queue.back() { + if nums[index] >= v { + break; + } + // 如果队列第一个元素比当前元素小,那么就把队列第一个元素弹出 + queue.pop_back(); + } + queue.push_back(i); + if i >= k as usize - 1 { + res.push(nums[queue[0]]); + } + } + res + } +} +``` +

From 31fa56d22947167a37ad767134ea98055314f367 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 31 Oct 2022 17:48:33 +0800 Subject: [PATCH 09/16] =?UTF-8?q?0347.=E5=89=8DK=E4=B8=AA=E9=AB=98?= =?UTF-8?q?=E9=A2=91=E5=85=83=E7=B4=A0.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 47456455..56dbaa33 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -487,7 +487,34 @@ object Solution { .map(_._1) } } +``` +rust: 小根堆 + +```rust +use std::cmp::Reverse; +use std::collections::{BinaryHeap, HashMap}; +impl Solution { + pub fn top_k_frequent(nums: Vec, k: i32) -> Vec { + let mut hash = HashMap::new(); + let mut heap = BinaryHeap::with_capacity(k as usize); + nums.into_iter().for_each(|k| { + *hash.entry(k).or_insert(0) += 1; + }); + + for (k, v) in hash { + if heap.len() == heap.capacity() { + if *heap.peek().unwrap() < (Reverse(v), k) { + continue; + } else { + heap.pop(); + } + } + heap.push((Reverse(v), k)); + } + heap.into_iter().map(|(_, k)| k).collect() + } +} ```

From 7803388377332c18559fa19296b064aa076a1f1b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 5 Nov 2022 14:30:53 +0800 Subject: [PATCH 10/16] =?UTF-8?q?update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树理论基础.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index e377626c..d520273a 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -270,6 +270,29 @@ class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) var right: TreeNode = _right } ``` + +rust: + +```rust +#[derive(Debug, PartialEq, Eq)] +pub struct TreeNode { + pub val: i32, + pub left: Option>>, + pub right: Option>>, +} + +impl TreeNode { + #[inline] + pub fn new(val: i32) -> Self { + TreeNode { + val, + left: None, + right: None, + } + } +} +``` +

From 72b6043daa4956cf6d0045e279792f1c35dd78b7 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 5 Nov 2022 14:36:15 +0800 Subject: [PATCH 11/16] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树理论基础.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index d520273a..03422960 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -275,15 +275,15 @@ rust: ```rust #[derive(Debug, PartialEq, Eq)] -pub struct TreeNode { - pub val: i32, - pub left: Option>>, - pub right: Option>>, +pub struct TreeNode { + pub val: T, + pub left: Option>>>, + pub right: Option>>>, } -impl TreeNode { +impl TreeNode { #[inline] - pub fn new(val: i32) -> Self { + pub fn new(val: T) -> Self { TreeNode { val, left: None, From c64c013b9b157b99ac6a87308a2e4352abbd989f Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 5 Nov 2022 16:24:41 +0800 Subject: [PATCH 12/16] =?UTF-8?q?update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的递归遍历.md | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 03304caf..78861040 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -525,6 +525,46 @@ object Solution { } } ``` + +rust: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn preorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + Self::traverse(&root, &mut res); + res + } + +//前序遍历 + pub fn traverse(root: &Option>>, res: &mut Vec) { + if let Some(node) = root { + res.push(node.borrow().val); + Self::traverse(&node.borrow().left, res); + Self::traverse(&node.borrow().right, res); + } + } +//后序遍历 + pub fn traverse(root: &Option>>, res: &mut Vec) { + if let Some(node) = root { + Self::traverse(&node.borrow().left, res); + Self::traverse(&node.borrow().right, res); + res.push(node.borrow().val); + } + } +//中序遍历 + pub fn traverse(root: &Option>>, res: &mut Vec) { + if let Some(node) = root { + Self::traverse(&node.borrow().left, res); + res.push(node.borrow().val); + Self::traverse(&node.borrow().right, res); + } + } +} +``` +

From 51d8be645aab9f15101c759535785492c4162e58 Mon Sep 17 00:00:00 2001 From: Zeeland <287017217@qq.com> Date: Sat, 5 Nov 2022 18:08:43 +0800 Subject: [PATCH 13/16] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除一行不必要的print,简化for循环代码 --- problems/0242.有效的字母异位词.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index f8ebc545..b7100b52 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -123,12 +123,11 @@ Python: class Solution: def isAnagram(self, s: str, t: str) -> bool: record = [0] * 26 - for i in range(len(s)): + for i in s: #并不需要记住字符a的ASCII,只要求出一个相对数值就可以了 - record[ord(s[i]) - ord("a")] += 1 - print(record) - for i in range(len(t)): - record[ord(t[i]) - ord("a")] -= 1 + record[ord(i) - ord("a")] += 1 + for i in t: + record[ord(i) - ord("a")] -= 1 for i in range(26): if record[i] != 0: #record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。 From 4ecf4714c71f2ca6ea3179a161beec76adfc118b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 7 Nov 2022 00:08:02 +0800 Subject: [PATCH 14/16] =?UTF-8?q?update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的迭代遍历.md | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index a20f11cb..2f67c323 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -640,6 +640,60 @@ object Solution { } } ``` + +rust: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + //前序 + pub fn preorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + let mut stack = vec![root]; + while !stack.is_empty() { + if let Some(node) = stack.pop().unwrap() { + res.push(node.borrow().val); + stack.push(node.borrow().right.clone()); + stack.push(node.borrow().left.clone()); + } + } + res + } + //中序 + pub fn inorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + let mut stack = vec![]; + let mut node = root; + + while !stack.is_empty() || node.is_some() { + while let Some(n) = node { + node = n.borrow().left.clone(); + stack.push(n); + } + if let Some(n) = stack.pop() { + res.push(n.borrow().val); + node = n.borrow().right.clone(); + } + } + res + } + //后序 + pub fn postorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + let mut stack = vec![root]; + while !stack.is_empty() { + if let Some(node) = stack.pop().unwrap() { + res.push(node.borrow().val); + stack.push(node.borrow().left.clone()); + stack.push(node.borrow().right.clone()); + } + } + res.into_iter().rev().collect() + } +} +``` +

From 1e40f295a9906df7b5c4308cb1fe733e73bf02b4 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 7 Nov 2022 17:24:44 +0800 Subject: [PATCH 15/16] =?UTF-8?q?update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md=20abo?= =?UTF-8?q?ut=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 030d3ae1..6e768ec7 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -666,6 +666,83 @@ object Solution { } } ``` + +rust: + +```rust +impl Solution{ + // 前序 + pub fn preorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + let mut stack = vec![]; + if root.is_some(){ + stack.push(root); + } + while !stack.is_empty(){ + if let Some(node) = stack.pop().unwrap(){ + if node.borrow().right.is_some(){ + stack.push(node.borrow().right.clone()); + } + if node.borrow().left.is_some(){ + stack.push(node.borrow().left.clone()); + } + stack.push(Some(node)); + stack.push(None); + }else{ + res.push(stack.pop().unwrap().unwrap().borrow().val); + } + } + res + } + // 中序 + pub fn inorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + let mut stack = vec![]; + if root.is_some() { + stack.push(root); + } + while !stack.is_empty() { + if let Some(node) = stack.pop().unwrap() { + if node.borrow().right.is_some() { + stack.push(node.borrow().right.clone()); + } + stack.push(Some(node.clone())); + stack.push(None); + if node.borrow().left.is_some() { + stack.push(node.borrow().left.clone()); + } + } else { + res.push(stack.pop().unwrap().unwrap().borrow().val); + } + } + res + } + // 后序 + pub fn postorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + let mut stack = vec![]; + if root.is_some() { + stack.push(root); + } + while !stack.is_empty() { + if let Some(node) = stack.pop().unwrap() { + stack.push(Some(node.clone())); + stack.push(None); + if node.borrow().right.is_some() { + stack.push(node.borrow().right.clone()); + } + if node.borrow().left.is_some() { + stack.push(node.borrow().left.clone()); + } + } else { + res.push(stack.pop().unwrap().unwrap().borrow().val); + } + } + res + } +} +``` +

From fe46bb43d2272c656c0744b88081b72487276798 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 8 Nov 2022 12:51:43 +0800 Subject: [PATCH 16/16] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 6e768ec7..69c23e5c 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -695,7 +695,7 @@ impl Solution{ res } // 中序 - pub fn inorder_traversal(root: Option>>) -> Vec { + pub fn inorder_traversal(root: Option>>) -> Vec { let mut res = vec![]; let mut stack = vec![]; if root.is_some() {