Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Sep 18, 2021
1 parent e9f48c2 commit 483b806
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 67 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@

题目分类大纲如下:

<img src='https://img-blog.csdnimg.cn/20210220152245584.png' width=600 alt='贪心算法大纲'> </img></div>

<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20210917104315.png' width=600 alt='贪心算法大纲'> </img></div>

1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md)
2. [贪心算法:分发饼干](./problems/0455.分发饼干.md)
Expand Down
23 changes: 11 additions & 12 deletions problems/0017.电话号码的字母组合.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

如果输入"233"呢,那么就三层for循环,如果"2333"呢,就四层for循环.......

大家应该感觉出和[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)遇到的一样的问题,就是这for循环的层数如何写出来,此时又是回溯法登场的时候了。
大家应该感觉出和[77.组合](https://programmercarl.com/0077.组合.html)遇到的一样的问题,就是这for循环的层数如何写出来,此时又是回溯法登场的时候了。

理解本题后,要解决如下三个问题:

Expand Down Expand Up @@ -75,7 +75,7 @@ const string letterMap[10] = {

再来看参数,参数指定是有题目中给的string digits,然后还要有一个参数就是int型的index。

注意这个index可不是 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中的startIndex了。
注意这个index可不是 [77.组合](https://programmercarl.com/0077.组合.html)[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)中的startIndex了。

这个index是记录遍历第几个数字了,就是用来遍历digits的(题目中给出数字字符串),同时index也表示树的深度。

Expand Down Expand Up @@ -110,7 +110,7 @@ if (index == digits.size()) {

然后for循环来处理这个字符集,代码如下:

```
```CPP
int digit = digits[index] - '0'; // 将index指向的数字转为int
string letters = letterMap[digit]; // 取数字对应的字符集
for (int i = 0; i < letters.size(); i++) {
Expand All @@ -137,7 +137,7 @@ for (int i = 0; i < letters.size(); i++) {
关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中的回溯法模板,不难写出如下C++代码:


```c++
```CPP
// 版本一
class Solution {
private:
Expand Down Expand Up @@ -183,7 +183,7 @@ public:
一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方)
```c++
```CPP
// 版本二
class Solution {
private:
Expand Down Expand Up @@ -236,10 +236,10 @@ public:



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


Java
## Java
```Java
class Solution {

Expand Down Expand Up @@ -281,7 +281,7 @@ class Solution {
}
```

Python
## Python

```Python
class Solution:
Expand Down Expand Up @@ -340,10 +340,9 @@ class Solution:
```


Go:

## Go

> 主要在于递归中传递下一个数字
主要在于递归中传递下一个数字

```go
func letterCombinations(digits string) []string {
Expand Down Expand Up @@ -382,7 +381,7 @@ func recursion(tempString ,digits string, Index int,digitsMap [10]string, res *[
}
```

javaScript
## javaScript

```js
var letterCombinations = function(digits) {
Expand Down
64 changes: 64 additions & 0 deletions problems/0037.解数独.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,70 @@ var solveSudoku = function(board) {
};
```

C:

```C
bool isValid(char** board, int row, int col, int k) {
/* 判断当前行是否有重复元素 */
for (int i = 0; i < 9; i++) {
if (board[i][col] == k) {
return false;
}
}
/* 判断当前列是否有重复元素 */
for (int j = 0; j < 9; j++) {
if (board[row][j] == k) {
return false;
}
}
/* 计算当前9宫格左上角的位置 */
int startRow = (row / 3) * 3;
int startCol = (col / 3) * 3;
/* 判断当前元素所在九宫格是否有重复元素 */
for (int i = startRow; i < startRow + 3; i++) {
for (int j = startCol; j < startCol + 3; j++) {
if (board[i][j] == k) {
return false;
}
}
}
/* 满足条件,返回true */
return true;
}

bool backtracking(char** board, int boardSize, int* boardColSize) {
/* 从上到下、从左到右依次遍历输入数组 */
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < *boardColSize; j++) {
/* 遇到数字跳过 */
if (board[i][j] != '.') {
continue;
}
/* 依次将数组1到9填入当前位置 */
for (int k = '1'; k <= '9'; k++) {
/* 判断当前位置是否与满足条件,是则进入下一层 */
if (isValid(board, i, j, k)) {
board[i][j] = k;
/* 判断下一层递归之后是否找到一种解法,是则返回true */
if (backtracking(board, boardSize, boardColSize)) {
return true;
}
/* 回溯,将当前位置清零 */
board[i][j] = '.';
}
}
/* 若填入的9个数均不满足条件,返回false,说明此解法无效 */
return false;
}
}
/* 遍历完所有的棋盘,没有返回false,说明找到了解法,返回true */
return true;
}

void solveSudoku(char** board, int boardSize, int* boardColSize) {
bool res = backtracking(board, boardSize, boardColSize);
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down
8 changes: 4 additions & 4 deletions problems/0047.全排列II.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
**如果对回溯算法基础还不了解的话,我还特意录制了一期视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。


这道题目和[回溯算法:排列问题!](https://programmercarl.com/0046.全排列.html)的区别在与**给定一个可包含重复数字的序列**,要返回**所有不重复的全排列**
这道题目和[46.全排列](https://programmercarl.com/0046.全排列.html)的区别在与**给定一个可包含重复数字的序列**,要返回**所有不重复的全排列**

这里又涉及到去重了。

[回溯算法:求组合总和(三)](https://programmercarl.com/0040.组合总和II.html)[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)我们分别详细讲解了组合问题和子集问题如何去重。
[40.组合总和II](https://programmercarl.com/0040.组合总和II.html)[90.子集II](https://programmercarl.com/0090.子集II.html)我们分别详细讲解了组合问题和子集问题如何去重。

那么排列问题其实也是一样的套路。

Expand All @@ -51,11 +51,11 @@

**一般来说:组合问题和排列问题是在树形结构的叶子节点上收集结果,而子集问题就是取树上所有节点的结果**

[回溯算法:排列问题!](https://programmercarl.com/0046.全排列.html)中已经详解讲解了排列问题的写法,在[回溯算法:求组合总和(三)](https://programmercarl.com/0040.组合总和II.html)[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)中详细讲解的去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下:
[46.全排列](https://programmercarl.com/0046.全排列.html)中已经详解讲解了排列问题的写法,在[40.组合总和II](https://programmercarl.com/0040.组合总和II.html)[90.子集II](https://programmercarl.com/0090.子集II.html)中详细讲解的去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下:

## C++代码

```
```CPP
class Solution {
private:
vector<vector<int>> result;
Expand Down
2 changes: 1 addition & 1 deletion problems/0051.N皇后.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ for (int col = 0; col < n; col++) {

代码如下:

```
```CPP
bool isValid(int row, int col, vector<string>& chessboard, int n) {
int count = 0;
// 检查列
Expand Down
2 changes: 2 additions & 0 deletions problems/0053.最大子序和.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public:

当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。

不少同学认为 如果输入用例都是-1,或者 都是负数,这个贪心算法跑出来的结果是0, 这是**又一次证明脑洞模拟不靠谱的经典案例**,建议大家把代码运行一下试一试,就知道了,也会理解 为什么 result 要初始化为最小负数了。

## 动态规划

当然本题还可以用动态规划来做,当前[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png)主要讲解贪心系列,后续到动态规划系列的时候会详细讲解本题的dp方法。
Expand Down
Loading

0 comments on commit 483b806

Please sign in to comment.