Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-greedy

This commit is contained in:
Yuhao Ju
2022-12-17 20:56:29 +08:00
committed by GitHub
8 changed files with 81 additions and 9 deletions

View File

@ -514,21 +514,21 @@ function combine(n: number, k: number): number[][] {
```Rust
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, n: i32, k: i32, startIndex: i32) {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, n: i32, k: i32, start_index: i32) {
let len= path.len() as i32;
if len == k{
result.push(path.to_vec());
return;
}
for i in startIndex..= n {
for i in start_index..= n {
path.push(i);
Self::backtracking(result, path, n, k, i+1);
path.pop();
}
}
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
let mut result = vec![];
let mut path = vec![];
Self::backtracking(&mut result, &mut path, n, k, 1);
result
}
@ -538,22 +538,22 @@ impl Solution {
剪枝
```Rust
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, n: i32, k: i32, startIndex: i32) {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, n: i32, k: i32, start_index: i32) {
let len= path.len() as i32;
if len == k{
result.push(path.to_vec());
return;
}
// 此处剪枝
for i in startIndex..= n - (k - len) + 1 {
for i in start_index..= n - (k - len) + 1 {
path.push(i);
Self::backtracking(result, path, n, k, i+1);
path.pop();
}
}
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
let mut result = vec![];
let mut path = vec![];
Self::backtracking(&mut result, &mut path, n, k, 1);
result
}

View File

@ -637,6 +637,55 @@ object Solution {
}
```
## rust
递归:
```rust
impl Solution {
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
Self::valid_bst(i64::MIN, i64::MAX, root)
}
pub fn valid_bst(low: i64, upper: i64, root: Option<Rc<RefCell<TreeNode>>>) -> bool {
if root.is_none() {
return true;
}
let root = root.as_ref().unwrap().borrow();
if root.val as i64 <= low || root.val as i64 >= upper {
return false;
}
Self::valid_bst(low, root.val as i64, root.left.clone())
&& Self::valid_bst(root.val as i64, upper, root.right.clone())
}
}
```
辅助数组:
```rust
impl Solution {
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
let mut vec = vec![];
Self::valid_bst(root, &mut vec);
for i in 1..vec.len() {
if vec[i] <= vec[i - 1] {
return false;
}
}
true
}
pub fn valid_bst(root: Option<Rc<RefCell<TreeNode>>>, mut v: &mut Vec<i64>) {
if root.is_none() {
return;
}
let node = root.as_ref().unwrap().borrow();
Self::valid_bst(node.left.clone(), v);
v.push(node.val as i64);
Self::valid_bst(node.right.clone(), v);
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -19,6 +19,10 @@
![108.将有序数组转换为二叉搜索树](https://img-blog.csdnimg.cn/20201022164420763.png)
# 算法公开课
**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
# 思路
做这道题目之前大家可以了解一下这几道:

View File

@ -35,8 +35,11 @@
* 所有节点的值都是唯一的。
* p、q 为不同节点且均存在于给定的二叉搜索树中。
# 思路
# 算法公开课
**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
# 思路
做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道利用回溯从底向上搜索遇到一个节点的左子树里有p右子树里有q那么当前节点就是最近公共祖先。

View File

@ -23,6 +23,10 @@
![450.删除二叉搜索树中的节点](https://img-blog.csdnimg.cn/20201020171048265.png)
# 算法公开课
**《代码随想录》算法视频公开课:[调整二叉树的结构最难!| LeetCode450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
# 思路
搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。

View File

@ -43,6 +43,10 @@
* 树中的所有值 互不相同 。
* 给定的树为二叉搜索树。
# 算法公开课
**《代码随想录》算法视频公开课:[普大喜奔!二叉树章节已全部更完啦!| LeetCode538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
# 思路
一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后再遍历其他节点累加?怎么一想这么麻烦呢。

View File

@ -18,6 +18,10 @@
![669.修剪二叉搜索树1](https://img-blog.csdnimg.cn/20201014173219142.png)
# 算法公开课
**《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
# 思路
相信看到这道题目大家都感觉是一道简单题事实上leetcode上也标明是简单

View File

@ -22,6 +22,10 @@
* -10^8 <= val <= 10^8
* 新值和原始二叉搜索树中的任意节点值都不同
# 算法公开课
**《代码随想录》算法视频公开课:[原来这么简单? | LeetCode701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
# 思路
这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。