From 43275d8e9cf593839bb505353f79e42c3cba7bb7 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 20:51:03 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Update=200222.=E5=AE=8C=E5=85=A8=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA=E6=95=B0?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0222.完全二叉树的节点个数.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 6754864d..bfb9ac0f 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -797,6 +797,23 @@ object Solution { } ``` +rust: + +// 递归 +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn count_nodes(root: Option>>) -> i32 { + if root.is_none() { + return 0; + } + 1 + Self::count_nodes(Rc::clone(root.as_ref().unwrap()).borrow().left.clone()) + + Self::count_nodes(root.unwrap().borrow().right.clone()) + } +} +``` +

From 933cada6dbcd4169e3345828e886a127688db619 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 21:19:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?Update=200222.=E5=AE=8C=E5=85=A8=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA=E6=95=B0?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index bfb9ac0f..8a3a15f3 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -814,6 +814,35 @@ impl Solution { } ``` +// 迭代 +```rust +use std::rc::Rc; +use std::cell::RefCell; +use std::collections::VecDeque; +impl Solution { + pub fn count_nodes(root: Option>>) -> i32 { + let mut res = 0; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + 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 += 1; + } + } + res + } +} +``` +

From 1af721e1ed561764b081c8d8ddc25d7c614fe7fd Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 21:58:16 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=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 --- problems/0110.平衡二叉树.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 6e7c8c53..9751f2dd 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -810,6 +810,38 @@ func getHeight(_ root: TreeNode?) -> Int { } ``` +### rust + +递归 + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn is_balanced(root: Option>>) -> bool { + Self::get_depth(root) != -1 + } + pub fn get_depth(root: Option>>) -> i32 { + if root.is_none() { + return 0; + } + let right = Self::get_depth(root.as_ref().unwrap().borrow().left.clone()); + let left = Self::get_depth(root.unwrap().borrow().right.clone()); + if right == -1 { + return -1; + } + if left == -1 { + return -1; + } + if (right - left).abs() > 1 { + return -1; + } + + 1 + right.max(left) + } +} +``` +

From 9b2f1700ca45604662b4e10bd2769b5026d3b1f9 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 22:45:20 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 297acb60..0fb7ed0d 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -794,6 +794,34 @@ object Solution { } } ``` + +rust: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn binary_tree_paths(root: Option>>) -> Vec { + let mut res = vec![]; + Self::recur(&root, String::from(""), &mut res); + res + } + pub fn recur(node: &Option>>, mut path: String, res: &mut Vec) { + let r = node.as_ref().unwrap().borrow(); + path += format!("{}", r.val).as_str(); + if r.left.is_none() && r.right.is_none() { + res.push(path.to_string()); + return; + } + if r.left.is_some() { + Self::recur(&r.left, path.clone() + "->", res); + } + if r.right.is_some() { + Self::recur(&r.right, path + "->", res); + } + } +} +```

From 6e01224ee38f0f97fc1e455233948622e1dae6a0 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 22:46:07 +0800 Subject: [PATCH 5/7] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 0fb7ed0d..80ce518a 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -798,8 +798,7 @@ object Solution { rust: ```rust -use std::cell::RefCell; -use std::rc::Rc; +// 遍历 impl Solution { pub fn binary_tree_paths(root: Option>>) -> Vec { let mut res = vec![]; From 299fab57663088a2aa12e958c36b2b04a36c0e1e Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 22:56:11 +0800 Subject: [PATCH 6/7] =?UTF-8?q?Update=20problems/0257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 80ce518a..a6e14238 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -798,7 +798,7 @@ object Solution { rust: ```rust -// 遍历 +// 递归 impl Solution { pub fn binary_tree_paths(root: Option>>) -> Vec { let mut res = vec![]; From 6772a996bba54cf0741b53a5306a51d5664ffadd Mon Sep 17 00:00:00 2001 From: wangshihua Date: Wed, 16 Nov 2022 09:27:42 +0800 Subject: [PATCH 7/7] =?UTF-8?q?feat:=E4=BF=AE=E6=94=B9=2027=20=E9=A2=98=20?= =?UTF-8?q?python=20=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=B9=B6=E5=A2=9E=E5=8A=A0=E5=BF=AB=E6=85=A2?= =?UTF-8?q?=E6=8C=87=E9=92=88=E7=9A=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 38 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e1de7243..35d0e2c4 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -77,7 +77,7 @@ public: 双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。** -定义快慢指针 +定义快慢指针 * 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组 * 慢指针:指向更新 新数组下标的位置 @@ -196,26 +196,44 @@ class Solution { Python: -```python3 +```python 3 class Solution: def removeElement(self, nums: List[int], val: int) -> int: - if nums is None or len(nums)==0: - return 0 + if nums is None or len(nums)==0: + return 0 l=0 r=len(nums)-1 - while l int: + # 快慢指针 + fast = 0 # 快指针 + slow = 0 # 慢指针 + size = len(nums) + while fast < size: # 不加等于是因为,a = size 时,nums[a] 会越界 + # slow 用来收集不等于 val 的值,如果 fast 对应值不等于 val,则把它与 slow 替换 + if nums[fast] != val: + nums[slow] = nums[fast] + slow += 1 + fast += 1 + return slow +``` + + + Go: ```go @@ -268,7 +286,7 @@ Ruby: ```ruby def remove_element(nums, val) i = 0 - nums.each_index do |j| + nums.each_index do |j| if nums[j] != val nums[i] = nums[j] i+=1 @@ -342,7 +360,7 @@ int removeElement(int* nums, int numsSize, int val){ if(nums[fast] != val) { //将其挪到慢指针指向的位置,慢指针+1 nums[slow++] = nums[fast]; - } + } } //最后慢指针的大小就是新的数组的大小 return slow;