mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Update
This commit is contained in:
@ -12,6 +12,12 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
# 算法视频讲解
|
||||||
|
|
||||||
|
* [KMP算法(理论篇)B站视频](https://www.bilibili.com/video/BV1PD4y1o7nd)
|
||||||
|
* [KMP算法(代码篇)B站视频](https://www.bilibili.com/video/BV1M5411j7Xx)
|
||||||
|
* [回溯算法(理论篇)B站视频](https://www.bilibili.com/video/BV1cy4y167mM)
|
||||||
|
|
||||||
# 算法文章精选
|
# 算法文章精选
|
||||||
|
|
||||||
**提示:在电脑端看如下文章的,看不到文章的评论区,建议在手机端「代码随想录」公众号里也翻一下对应的文章,评论区有录友们的打卡总结,相信会和你有不少共鸣!**
|
**提示:在电脑端看如下文章的,看不到文章的评论区,建议在手机端「代码随想录」公众号里也翻一下对应的文章,评论区有录友们的打卡总结,相信会和你有不少共鸣!**
|
||||||
@ -150,6 +156,8 @@
|
|||||||
* [本周小结!(回溯算法系列三)](https://mp.weixin.qq.com/s/tLkt9PSo42X60w8i94ViiA)
|
* [本周小结!(回溯算法系列三)](https://mp.weixin.qq.com/s/tLkt9PSo42X60w8i94ViiA)
|
||||||
* [本周小结!(回溯算法系列三)续集](https://mp.weixin.qq.com/s/kSMGHc_YpsqL2j-jb_E_Ag)
|
* [本周小结!(回溯算法系列三)续集](https://mp.weixin.qq.com/s/kSMGHc_YpsqL2j-jb_E_Ag)
|
||||||
* [视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg)
|
* [视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg)
|
||||||
|
* [回溯算法:重新安排行程](https://mp.weixin.qq.com/s/3kmbS4qDsa6bkyxR92XCTA)
|
||||||
|
* [回溯算法:N皇后问题](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg)
|
||||||
|
|
||||||
|
|
||||||
(持续更新中....)
|
(持续更新中....)
|
||||||
@ -356,6 +364,7 @@
|
|||||||
|[0127.单词接龙](https://github.com/youngyangyang04/leetcode/blob/master/problems/0127.单词接龙.md) |广度优先搜索 |中等|**广度优先搜索**|
|
|[0127.单词接龙](https://github.com/youngyangyang04/leetcode/blob/master/problems/0127.单词接龙.md) |广度优先搜索 |中等|**广度优先搜索**|
|
||||||
|[0129.求根到叶子节点数字之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0129.求根到叶子节点数字之和.md) |二叉树 |中等|**递归/回溯** 递归里隐藏着回溯,和113.路径总和II类似|
|
|[0129.求根到叶子节点数字之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0129.求根到叶子节点数字之和.md) |二叉树 |中等|**递归/回溯** 递归里隐藏着回溯,和113.路径总和II类似|
|
||||||
|[0131.分割回文串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0131.分割回文串.md) |回溯 |中等|**回溯**|
|
|[0131.分割回文串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0131.分割回文串.md) |回溯 |中等|**回溯**|
|
||||||
|
|[0135.分发糖果](https://github.com/youngyangyang04/leetcode/blob/master/problems/0135.分发糖果.md) |贪心 |困难|**贪心**好题目|
|
||||||
|[0141.环形链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0141.环形链表.md) |链表 |简单|**快慢指针/双指针**|
|
|[0141.环形链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0141.环形链表.md) |链表 |简单|**快慢指针/双指针**|
|
||||||
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
|
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
|
||||||
|[0143.重排链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0143.重排链表.md) |链表 |中等|**快慢指针/双指针** 也可以用数组,双向队列模拟,考察链表综合操作的好题|
|
|[0143.重排链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0143.重排链表.md) |链表 |中等|**快慢指针/双指针** 也可以用数组,双向队列模拟,考察链表综合操作的好题|
|
||||||
|
BIN
pics/51.N皇后.png
BIN
pics/51.N皇后.png
Binary file not shown.
Before Width: | Height: | Size: 165 KiB After Width: | Height: | Size: 162 KiB |
@ -89,7 +89,7 @@ void backtracking(int n, int row, vector<string>& chessboard) {
|
|||||||
* 递归终止条件
|
* 递归终止条件
|
||||||
|
|
||||||
在如下树形结构中:
|
在如下树形结构中:
|
||||||

|

|
||||||
|
|
||||||
可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。
|
可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。
|
||||||
|
|
||||||
|
@ -36,7 +36,8 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
|
|||||||
class Solution {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
int count = 0;
|
int count = 0;
|
||||||
void backtracking(int n, int row, vector<string>& chessboard, vector<vector<string>>& result) {
|
vector<vector<string>> result;
|
||||||
|
void backtracking(int n, int row, vector<string>& chessboard) {
|
||||||
if (row == n) {
|
if (row == n) {
|
||||||
count++;
|
count++;
|
||||||
return;
|
return;
|
||||||
@ -44,7 +45,7 @@ void backtracking(int n, int row, vector<string>& chessboard, vector<vector<stri
|
|||||||
for (int col = 0; col < n; col++) {
|
for (int col = 0; col < n; col++) {
|
||||||
if (isValid(row, col, chessboard, n)) {
|
if (isValid(row, col, chessboard, n)) {
|
||||||
chessboard[row][col] = 'Q'; // 放置皇后
|
chessboard[row][col] = 'Q'; // 放置皇后
|
||||||
backtracking(n, row + 1, chessboard, result);
|
backtracking(n, row + 1, chessboard);
|
||||||
chessboard[row][col] = '.'; // 回溯
|
chessboard[row][col] = '.'; // 回溯
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,9 +75,10 @@ bool isValid(int row, int col, vector<string>& chessboard, int n) {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
int totalNQueens(int n) {
|
int totalNQueens(int n) {
|
||||||
|
result.clear();
|
||||||
std::vector<std::string> chessboard(n, std::string(n, '.'));
|
std::vector<std::string> chessboard(n, std::string(n, '.'));
|
||||||
vector<vector<string>> result;
|
vector<vector<string>> result;
|
||||||
backtracking(n, 0, chessboard, result);
|
backtracking(n, 0, chessboard);
|
||||||
return count;
|
return count;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
代码如下:
|
代码如下:
|
||||||
|
|
||||||
```
|
```C++
|
||||||
// 从前向后
|
// 从前向后
|
||||||
for (int i = 1; i < ratings.size(); i++) {
|
for (int i = 1; i < ratings.size(); i++) {
|
||||||
if (ratings[i] > ratings[i - 1]) candyVec[i] = candyVec[i - 1] + 1;
|
if (ratings[i] > ratings[i - 1]) candyVec[i] = candyVec[i - 1] + 1;
|
||||||
@ -37,7 +37,7 @@ for (int i = 1; i < ratings.size(); i++) {
|
|||||||
|
|
||||||
所以代码如下:
|
所以代码如下:
|
||||||
|
|
||||||
```
|
```C++
|
||||||
// 从后向前
|
// 从后向前
|
||||||
for (int i = ratings.size() - 2; i >= 0; i--) {
|
for (int i = ratings.size() - 2; i >= 0; i--) {
|
||||||
if (ratings[i] > ratings[i + 1] ) {
|
if (ratings[i] > ratings[i + 1] ) {
|
||||||
@ -46,21 +46,8 @@ for (int i = ratings.size() - 2; i >= 0; i--) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
* 将问题分解为若干个子问题
|
整体代码如下:
|
||||||
* 找出适合的贪心策略
|
```C++
|
||||||
* 求解每一个子问题的最优解
|
|
||||||
* 将局部最优解堆叠成全局最优解
|
|
||||||
|
|
||||||
* 分解为子问题
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
这道题目上来也是没什么思路啊
|
|
||||||
|
|
||||||
|
|
||||||
这道题目不好想啊,贪心很巧妙
|
|
||||||
|
|
||||||
```
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int candy(vector<int>& ratings) {
|
int candy(vector<int>& ratings) {
|
||||||
@ -81,5 +68,9 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> **我是[程序员Carl](https://github.com/youngyangyang04),[组队刷题](https://img-blog.csdnimg.cn/20201115103410182.png)可以找我,本文[leetcode刷题攻略](https://github.com/youngyangyang04/leetcode-master)已收录,更多[精彩算法文章](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzUxNjY5NTYxNA==&action=getalbum&album_id=1485825793120387074&scene=173#wechat_redirect)尽在:[代码随想录](https://img-blog.csdnimg.cn/20200815195519696.png),关注后就会发现和「代码随想录」相见恨晚!**
|
||||||
|
|
||||||
|
**如果感觉题解对你有帮助,不要吝啬给一个👍吧!**
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user