Skip to content

Commit

Permalink
20210824
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Aug 24, 2021
1 parent cd4f67d commit 80dc277
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 267 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定)
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)

> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
> 2. **PDF版本**[「代码随想录」算法精讲 PDF 版本](https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ)
Expand Down Expand Up @@ -477,6 +478,11 @@
* [100.相同的树](./problems/0100.相同的树.md) 同101.对称二叉树 一个思路
* [116.填充每个节点的下一个右侧节点指针](./problems/0116.填充每个节点的下一个右侧节点指针.md)

## 回溯算法

* [52.N皇后II](./problems/0052.N皇后II.md)


## 贪心
* [649.Dota2参议院](./problems/0649.Dota2参议院.md) 有难度

Expand Down
97 changes: 97 additions & 0 deletions problems/0052.N皇后II.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

<p align="center">
<a href="https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ"><img src="https://img.shields.io/badge/PDF下载-代码随想录-blueviolet" alt=""></a>
<a href="https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a>
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
</p>
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>


# 52. N皇后II

题目链接:https://leetcode-cn.com/problems/n-queens-ii/

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

上图为 8 皇后问题的一种解法。
![51n皇后](https://img-blog.csdnimg.cn/20200821152118456.png)

给定一个整数 n,返回 n 皇后不同的解决方案的数量。

示例:

输入: 4
输出: 2
解释: 4 皇后问题存在如下两个不同的解法。
[
 [".Q..",  // 解法 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
]

# 思路


想看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别

# C++代码

```CPP
class Solution {
private:
int count = 0;
void backtracking(int n, int row, vector<string>& chessboard) {
if (row == n) {
count++;
return;
}
for (int col = 0; col < n; col++) {
if (isValid(row, col, chessboard, n)) {
chessboard[row][col] = 'Q'; // 放置皇后
backtracking(n, row + 1, chessboard);
chessboard[row][col] = '.'; // 回溯
}
}
}
bool isValid(int row, int col, vector<string>& chessboard, int n) {
int count = 0;
// 检查列
for (int i = 0; i < row; i++) { // 这是一个剪枝
if (chessboard[i][col] == 'Q') {
return false;
}
}
// 检查 45度角是否有皇后
for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {
if (chessboard[i][j] == 'Q') {
return false;
}
}
// 检查 135度角是否有皇后
for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (chessboard[i][j] == 'Q') {
return false;
}
}
return true;
}

public:
int totalNQueens(int n) {
std::vector<std::string> chessboard(n, std::string(n, '.'));
backtracking(n, 0, chessboard);
return count;

}
};
```

# 其他语言补充

33 changes: 33 additions & 0 deletions problems/0084.柱状图中最大的矩形.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,39 @@ public:

Java:

动态规划
```java
class Solution {
public int largestRectangleArea(int[] heights) {
int length = heights.length;
int[] minLeftIndex = new int [length];
int[] maxRigthIndex = new int [length];
// 记录左边第一个小于该柱子的下标
minLeftIndex[0] = -1 ;
for (int i = 1; i < length; i++) {
int t = i - 1;
// 这里不是用if,而是不断向右寻找的过程
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
minLeftIndex[i] = t;
}
// 记录每个柱子 右边第一个小于该柱子的下标
maxRigthIndex[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;
}
// 求和
int result = 0;
for (int i = 0; i < length; i++) {
int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
result = Math.max(sum, result);
}
return result;
}
}
```

Python:

动态规划
Expand Down
40 changes: 19 additions & 21 deletions problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>


## 98.验证二叉搜索树
# 98.验证二叉搜索树

题目地址:https://leetcode-cn.com/problems/validate-binary-search-tree/

Expand All @@ -22,7 +22,7 @@

![98.验证二叉搜索树](https://img-blog.csdnimg.cn/20210203144334501.png)

## 思路
# 思路

要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。

Expand Down Expand Up @@ -121,7 +121,7 @@ if (root->val > root->left->val && root->val < root->right->val) {
要定义一个longlong的全局变量,用来比较遍历的节点是否有序,因为后台测试数据中有int最小值,所以定义为longlong的类型,初始化为longlong最小值。
注意递归函数要有bool类型的返回值, 我们在[二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](https://mp.weixin.qq.com/s/6TWAVjxQ34kVqROWgcRFOg) 中讲了,只有寻找某一条边(或者一个节点)的时候,递归函数会有bool类型的返回值。
注意递归函数要有bool类型的返回值, 我们在[二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](https://mp.weixin.qq.com/s/EJr_nZ31TnvZmptBjkDGqA) 中讲了,只有寻找某一条边(或者一个节点)的时候,递归函数会有bool类型的返回值。
其实本题是同样的道理,我们在寻找一个不符合条件的节点,如果没有找到这个节点就遍历了整个树,如果找到不符合的节点了,立刻返回。
Expand Down Expand Up @@ -210,7 +210,8 @@ public:

## 迭代法

可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://mp.weixin.qq.com/s/c_zCrGHIVlBjUH_hJtghCg)[二叉树:前中后序迭代方式统一写法](https://mp.weixin.qq.com/s/WKg0Ty1_3SZkztpHubZPRg)

可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://mp.weixin.qq.com/s/OH7aCVJ5-Gi32PkNCoZk4A)[二叉树:前中后序迭代方式统一写法](https://mp.weixin.qq.com/s/ATQMPCpBlaAgrqdLDMVPZA)

迭代法中序遍历稍加改动就可以了,代码如下:

Expand Down Expand Up @@ -240,9 +241,10 @@ public:
};
```

[二叉树:二叉搜索树登场!](https://mp.weixin.qq.com/s/vsKrWRlETxCVsiRr8v_hHg)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。

## 总结
[二叉树:二叉搜索树登场!](https://mp.weixin.qq.com/s/muRjJulujBqZXW4apLkGpg)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。

# 总结

这道题目是一个简单题,但对于没接触过的同学还是有难度的。

Expand All @@ -251,10 +253,10 @@ public:
只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。


## 其他语言版本
# 其他语言版本


Java
## Java

```Java
class Solution {
Expand Down Expand Up @@ -336,16 +338,10 @@ class Solution {
}
```

Python
## Python

**递归** - 利用BST中序遍历特性,把树"压缩"成数组
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
# 思路: 利用BST中序遍历的特性.
Expand Down Expand Up @@ -395,8 +391,9 @@ class Solution:
return is_left_valid and is_right_valid
return __isValidBST(root)
```
```
# 迭代-中序遍历

```python
迭代-中序遍历
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
stack = []
Expand All @@ -415,7 +412,8 @@ class Solution:
return True

```
Go:
## Go

```Go
import "math"

Expand Down Expand Up @@ -458,9 +456,9 @@ func isValidBST(root *TreeNode) bool {
}
```

JavaScript版本
## JavaScript

> 辅助数组解决
辅助数组解决

```javascript
/**
Expand Down Expand Up @@ -493,7 +491,7 @@ var isValidBST = function (root) {
};
```

> 递归中解决
递归中解决

```javascript
/**
Expand Down
29 changes: 29 additions & 0 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,35 @@ public:

Java:

```java
class Solution {
public int minDepth(TreeNode root){
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int depth = 0;
while (!queue.isEmpty()){
int size = queue.size();
depth++;
TreeNode cur = null;
for (int i = 0; i < size; i++) {
cur = queue.poll();
//如果当前节点的左右孩子都为空,直接返回最小深度
if (cur.left == null && cur.right == null){
return depth;
}
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
}
return depth;
}
}
```



Python:

Expand Down
Loading

0 comments on commit 80dc277

Please sign in to comment.