mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-BinaryTree
This commit is contained in:
@ -77,7 +77,7 @@ public:
|
|||||||
|
|
||||||
双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
|
双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
|
||||||
|
|
||||||
定义快慢指针
|
定义快慢指针
|
||||||
|
|
||||||
* 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
|
* 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
|
||||||
* 慢指针:指向更新 新数组下标的位置
|
* 慢指针:指向更新 新数组下标的位置
|
||||||
@ -196,21 +196,26 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
```python3
|
|
||||||
|
``` python 3
|
||||||
class Solution:
|
class Solution:
|
||||||
def removeElement(self, nums: List[int], val: int) -> int:
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
# 快指针遍历元素
|
# 快慢指针
|
||||||
fast = 0
|
fast = 0 # 快指针
|
||||||
# 慢指针记录位置
|
slow = 0 # 慢指针
|
||||||
slow = 0
|
size = len(nums)
|
||||||
for fast in range(len(nums)):
|
while fast < size: # 不加等于是因为,a = size 时,nums[a] 会越界
|
||||||
|
# slow 用来收集不等于 val 的值,如果 fast 对应值不等于 val,则把它与 slow 替换
|
||||||
if nums[fast] != val:
|
if nums[fast] != val:
|
||||||
nums[slow] = nums[fast]
|
nums[slow] = nums[fast]
|
||||||
slow += 1
|
slow += 1
|
||||||
|
fast += 1
|
||||||
return slow
|
return slow
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
func removeElement(nums []int, val int) int {
|
func removeElement(nums []int, val int) int {
|
||||||
@ -262,7 +267,7 @@ Ruby:
|
|||||||
```ruby
|
```ruby
|
||||||
def remove_element(nums, val)
|
def remove_element(nums, val)
|
||||||
i = 0
|
i = 0
|
||||||
nums.each_index do |j|
|
nums.each_index do |j|
|
||||||
if nums[j] != val
|
if nums[j] != val
|
||||||
nums[i] = nums[j]
|
nums[i] = nums[j]
|
||||||
i+=1
|
i+=1
|
||||||
@ -336,7 +341,7 @@ int removeElement(int* nums, int numsSize, int val){
|
|||||||
if(nums[fast] != val) {
|
if(nums[fast] != val) {
|
||||||
//将其挪到慢指针指向的位置,慢指针+1
|
//将其挪到慢指针指向的位置,慢指针+1
|
||||||
nums[slow++] = nums[fast];
|
nums[slow++] = nums[fast];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//最后慢指针的大小就是新的数组的大小
|
//最后慢指针的大小就是新的数组的大小
|
||||||
return slow;
|
return slow;
|
||||||
|
@ -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<Rc<RefCell<TreeNode>>>) -> bool {
|
||||||
|
Self::get_depth(root) != -1
|
||||||
|
}
|
||||||
|
pub fn get_depth(root: Option<Rc<RefCell<TreeNode>>>) -> 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
@ -793,6 +793,52 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
rust:
|
||||||
|
|
||||||
|
// 递归
|
||||||
|
```rust
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
impl Solution {
|
||||||
|
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> 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<Rc<RefCell<TreeNode>>>) -> 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
@ -794,6 +794,33 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// 递归
|
||||||
|
impl Solution {
|
||||||
|
pub fn binary_tree_paths(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<String> {
|
||||||
|
let mut res = vec![];
|
||||||
|
Self::recur(&root, String::from(""), &mut res);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
pub fn recur(node: &Option<Rc<RefCell<TreeNode>>>, mut path: String, res: &mut Vec<String>) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user