This commit is contained in:
youngyangyang04
2020-11-17 17:12:33 +08:00
parent db295f4fca
commit c01adfa6bc
5 changed files with 24 additions and 22 deletions

View File

@ -12,6 +12,12 @@
![算法面试知识大纲](https://img-blog.csdnimg.cn/20200729181420491.png)
# 算法视频讲解
* [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/kSMGHc_YpsqL2j-jb_E_Ag)
* [视频来了!!带你学透回溯算法(理论篇)](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) |广度优先搜索 |中等|**广度优先搜索**|
|[0129.求根到叶子节点数字之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0129.求根到叶子节点数字之和.md) |二叉树 |中等|**递归/回溯** 递归里隐藏着回溯和113.路径总和II类似|
|[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) |链表 |简单|**快慢指针/双指针**|
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
|[0143.重排链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0143.重排链表.md) |链表 |中等|**快慢指针/双指针** 也可以用数组,双向队列模拟,考察链表综合操作的好题|

Binary file not shown.

Before

Width:  |  Height:  |  Size: 165 KiB

After

Width:  |  Height:  |  Size: 162 KiB

View File

@ -89,7 +89,7 @@ void backtracking(int n, int row, vector<string>& chessboard) {
* 递归终止条件
在如下树形结构中:
![51.N皇后](https://img-blog.csdnimg.cn/20201116173551789.png)
![51.N皇后](https://img-blog.csdnimg.cn/20201117165155239.png)
可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。

View File

@ -36,7 +36,8 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
class Solution {
private:
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) {
count++;
return;
@ -44,7 +45,7 @@ void backtracking(int n, int row, vector<string>& chessboard, vector<vector<stri
for (int col = 0; col < n; col++) {
if (isValid(row, col, chessboard, n)) {
chessboard[row][col] = 'Q'; // 放置皇后
backtracking(n, row + 1, chessboard, result);
backtracking(n, row + 1, chessboard);
chessboard[row][col] = '.'; // 回溯
}
}
@ -74,9 +75,10 @@ bool isValid(int row, int col, vector<string>& chessboard, int n) {
public:
int totalNQueens(int n) {
result.clear();
std::vector<std::string> chessboard(n, std::string(n, '.'));
vector<vector<string>> result;
backtracking(n, 0, chessboard, result);
backtracking(n, 0, chessboard);
return count;
}

View File

@ -11,7 +11,7 @@
代码如下:
```
```C++
// 从前向后
for (int i = 1; i < ratings.size(); i++) {
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--) {
if (ratings[i] > ratings[i + 1] ) {
@ -46,21 +46,8 @@ for (int i = ratings.size() - 2; i >= 0; i--) {
}
```
* 将问题分解为若干个子问题
* 找出适合的贪心策略
* 求解每一个子问题的最优解
* 将局部最优解堆叠成全局最优解
* 分解为子问题
这道题目上来也是没什么思路啊
这道题目不好想啊,贪心很巧妙
```
整体代码如下:
```C++
class Solution {
public:
int candy(vector<int>& ratings) {
@ -81,5 +68,9 @@ public:
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),关注后就会发现和「代码随想录」相见恨晚!**
**如果感觉题解对你有帮助,不要吝啬给一个👍吧!**