diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index a563a13f..19a600d1 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -77,7 +77,7 @@ public: 双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。** -定义快慢指针 +定义快慢指针 * 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组 * 慢指针:指向更新 新数组下标的位置 @@ -196,21 +196,26 @@ class Solution { Python: -```python3 + +``` python 3 class Solution: def removeElement(self, nums: List[int], val: int) -> int: - # 快指针遍历元素 - fast = 0 - # 慢指针记录位置 - slow = 0 - for fast in range(len(nums)): + # 快慢指针 + 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 func removeElement(nums []int, val int) int { @@ -262,7 +267,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 @@ -336,7 +341,7 @@ int removeElement(int* nums, int numsSize, int val){ if(nums[fast] != val) { //将其挪到慢指针指向的位置,慢指针+1 nums[slow++] = nums[fast]; - } + } } //最后慢指针的大小就是新的数组的大小 return slow; diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 83e4dc4f..492e25d9 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -805,6 +805,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) + } +} +``` +

diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 7e763bc6..b87877c9 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -793,6 +793,52 @@ 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()) + } +} +``` + +// 迭代 +```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 + } +} +``` +

diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 100531cb..2d796671 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -794,6 +794,33 @@ object Solution { } } ``` + +rust: + +```rust +// 递归 +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); + } + } +} +```