diff --git a/README.md b/README.md index b64bcea6..a66e5a30 100644 --- a/README.md +++ b/README.md @@ -69,13 +69,14 @@ ## 前序 -* [「代码随想录」学习社区](https://programmercarl.com/other/kstar.html) +* [做项目(多个C++、Java、Go、前端、测开项目)](https://programmercarl.com/other/kstar.html) * 编程语言 * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) * [编程语言基础课](https://kamacoder.com/courseshop.php) * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) + * [大厂算法笔试题](https://kamacoder.com/company.php) * 工具 * [一站式vim配置](https://github.com/youngyangyang04/PowerVim) @@ -180,7 +181,7 @@ 题目分类大纲如下: -二叉树大纲 +二叉树大纲 1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md) 2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md) @@ -221,7 +222,7 @@ 题目分类大纲如下: -回溯算法大纲 +回溯算法大纲 1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md) 2. [回溯算法:77.组合](./problems/0077.组合.md) @@ -251,7 +252,7 @@ 题目分类大纲如下: -贪心算法大纲 +贪心算法大纲 1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md) 2. [贪心算法:455.分发饼干](./problems/0455.分发饼干.md) @@ -385,9 +386,9 @@ 9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md) 10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md) 11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) -12. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) -13. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) -14. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) +12. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) +13. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) +14. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) 15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md) 16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md) 17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md) @@ -502,5 +503,5 @@ 添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦** -
+
diff --git a/pics/阿里云.png b/pics/阿里云.png deleted file mode 100644 index 79f41d6b..00000000 Binary files a/pics/阿里云.png and /dev/null differ diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index e982ae12..f9bea828 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1. 两数之和 @@ -85,10 +83,10 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候, 过程如下: -![过程一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220711202638.png) +![过程一](https://file.kamacoder.com/pics/20220711202638.png) -![过程二](https://code-thinking-1253855093.file.myqcloud.com/pics/20230220223536.png) +![过程二](https://file.kamacoder.com/pics/20230220223536.png) C++代码: @@ -557,7 +555,3 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){ } ``` -

- - - diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index b3d3b938..1e0667e5 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -108,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473.jpg) +![647.回文子串](https://file.kamacoder.com/pics/20210121171032473.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -142,7 +140,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951.jpg) +![647.回文子串1](https://file.kamacoder.com/pics/20210121171059951.jpg) **注意因为dp[i][j]的定义,所以j一定是大于等于i的,那么在填充dp[i][j]的时候一定是只填充右上半部分**。 @@ -731,8 +729,4 @@ public class Solution { ``` -

- - - diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 1685db74..52dbdab7 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -980,7 +978,3 @@ object Solution { } ``` -

- - - diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 9a320ca0..a35fd4e2 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 17.电话号码的字母组合 @@ -13,7 +11,7 @@ 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 -![17.电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/2020102916424043.png) +![17.电话号码的字母组合](https://file.kamacoder.com/pics/2020102916424043.png) 示例: * 输入:"23" @@ -66,7 +64,7 @@ const string letterMap[10] = { 例如:输入:"23",抽象为树形结构,如图所示: -![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123200304469.png) +![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201123200304469.png) 图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。 @@ -766,7 +764,3 @@ public class Solution ``` -

- - - diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index f3188b0f..bf7d3bd4 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 一样的道理,能解决四数之和 @@ -798,8 +796,4 @@ def four_sum(nums, target) end ``` -

- - - diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 16312d0f..9b2ba88e 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -18,7 +16,7 @@ 示例 1: -![19.删除链表的倒数第N个节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20210510085957392.png) +![19.删除链表的倒数第N个节点](https://file.kamacoder.com/pics/20210510085957392.png) 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] @@ -479,7 +477,3 @@ public class Solution { } } ``` -

- - - diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 2475138e..72824712 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -83,13 +81,13 @@ cd a/b/c/../../ 1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。 -![括号匹配1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020080915505387.png) +![括号匹配1](https://file.kamacoder.com/pics/2020080915505387.png) 2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。 -![括号匹配2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155107397.png) +![括号匹配2](https://file.kamacoder.com/pics/20200809155107397.png) 3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。 -![括号匹配3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155115779.png) +![括号匹配3](https://file.kamacoder.com/pics/20200809155115779.png) @@ -574,8 +572,4 @@ impl Solution { } ``` -

- - - diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 305bb7cc..b9494297 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 24. 两两交换链表中的节点 @@ -527,7 +525,3 @@ public ListNode SwapPairs(ListNode head) } ``` -

- - - diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 10817ba6..d01765ff 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 27. 移除元素 @@ -519,7 +517,3 @@ int removeElement(List nums, int val) { ``` -

- - - diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 63a08d96..b25cb301 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 在一个串中查找是否出现过另一个串,这是KMP的看家本领。 @@ -1520,7 +1518,3 @@ int strStr(char* haystack, char* needle) { } ``` -

- - - diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 48af8d0d..95bb1d89 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -269,7 +267,3 @@ var nextPermutation = function(nums) { -

- - - diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 16adcdf1..37248e48 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 34. 在排序数组中查找元素的第一个和最后一个位置 @@ -855,7 +853,3 @@ int* searchRange(int* nums, int numsSize, int target, int* returnSize){ ``` -

- - - diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index b5be9a5f..c9826fa2 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -43,7 +41,7 @@ 这道题目,要在数组中插入目标值,无非是这四种情况。 -![35_搜索插入位置3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232148471.png) +![35_搜索插入位置3](https://file.kamacoder.com/pics/20201216232148471.png) * 目标值在数组所有元素之前 * 目标值等于数组中某一个元素 @@ -84,14 +82,14 @@ public: 效率如下: -![35_搜索插入位置](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232127268.png) +![35_搜索插入位置](https://file.kamacoder.com/pics/20201216232127268.png) ### 二分法 既然暴力解法的时间复杂度是O(n),就要尝试一下使用二分查找法。 -![35_搜索插入位置4](https://code-thinking-1253855093.file.myqcloud.com/pics/202012162326354.png) +![35_搜索插入位置4](https://file.kamacoder.com/pics/202012162326354.png) 大家注意这道题目的前提是数组是有序数组,这也是使用二分查找的基础条件。 @@ -101,7 +99,7 @@ public: 大体讲解一下二分法的思路,这里来举一个例子,例如在这个数组中,使用二分法寻找元素为5的位置,并返回其下标。 -![35_搜索插入位置5](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232659199.png) +![35_搜索插入位置5](https://file.kamacoder.com/pics/20201216232659199.png) 二分查找涉及的很多的边界条件,逻辑比较简单,就是写不好。 @@ -152,7 +150,7 @@ public: * 空间复杂度:O(1) 效率如下: -![35_搜索插入位置2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020121623272877.png) +![35_搜索插入位置2](https://file.kamacoder.com/pics/2020121623272877.png) ### 二分法第二种写法 @@ -549,7 +547,3 @@ int searchInsert(int* nums, int numsSize, int target){ ``` -

- - - diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index 5f3f881c..5d2adb4d 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -20,11 +18,11 @@ 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 空白格用 '.' 表示。 -![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/202011171912586.png) +![解数独](https://file.kamacoder.com/pics/202011171912586.png) 一个数独。 -![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117191340669.png) +![解数独](https://file.kamacoder.com/pics/20201117191340669.png) 答案被标成红色。 @@ -54,7 +52,7 @@ 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131816104.png) +![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131816104.png) ### 回溯三部曲 @@ -85,7 +83,7 @@ bool backtracking(vector>& board) * 递归单层搜索逻辑 -![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131822254.png) +![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131822254.png) 在树形图中可以看出我们需要的是一个二维的递归 (一行一列) @@ -893,7 +891,3 @@ public class Solution ``` -

- - - diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index 92c68562..84672771 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -52,7 +50,7 @@ candidates 中的数字可以无限制重复被选取。 本题搜索的过程抽象成树形结构如下: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) 注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回! 而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。 @@ -87,7 +85,7 @@ void backtracking(vector& candidates, int target, int sum, int startIndex) 在如下树形结构中: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135337214.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135337214.png) 从叶子节点可以清晰看到,终止只有两种情况,sum大于target和sum等于target。 @@ -160,7 +158,7 @@ public: 在这个树形结构中: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135342472.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135342472.png) 以及上面的版本一的代码大家可以看到,对于sum已经大于target的情况,其实是依然进入了下一层递归,只是下一层递归结束判断的时候,会判断sum > target的话就返回。 @@ -173,7 +171,7 @@ public: 如图: -![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170809182.png) +![39.组合总和1](https://file.kamacoder.com/pics/20201223170809182.png) for循环剪枝代码如下: @@ -661,7 +659,3 @@ public class Solution -

- - - diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 22cf726d..f0cbc220 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -1,12 +1,8 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) -> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了! - # 40.组合总和II [力扣题目链接](https://leetcode.cn/problems/combination-sum-ii/) @@ -80,7 +76,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 选择过程树形结构如图所示: -![40.组合总和II](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000918.png) +![40.组合总和II](https://file.kamacoder.com/pics/20230310000918.png) 可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。 @@ -130,7 +126,7 @@ if (sum == target) { 这块比较抽象,如图: -![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000954.png) +![40.组合总和II1](https://file.kamacoder.com/pics/20230310000954.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -141,7 +137,7 @@ if (sum == target) { 而 used[i - 1] == true,说明是进入下一层递归,去下一个数,所以是树枝上,如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221021163812.png) +![](https://file.kamacoder.com/pics/20221021163812.png) **这块去重的逻辑很抽象,网上搜的题解基本没有能讲清楚的,如果大家之前思考过这个问题或者刷过这道题目,看到这里一定会感觉通透了很多!** @@ -807,7 +803,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 0484f830..1e6ec11b 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -1,13 +1,10 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) -> 这个图就是大厂面试经典题目,接雨水! 最常青藤的一道题,面试官百出不厌! # 42. 接雨水 @@ -50,10 +47,10 @@ 首先要明确,要按照行来计算,还是按照列来计算。 按照行来计算如图: -![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091118927.png) +![42.接雨水2](https://file.kamacoder.com/pics/20210402091118927.png) 按照列来计算如图: -![42.接雨水1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091208445.png) +![42.接雨水1](https://file.kamacoder.com/pics/20210402091208445.png) 一些同学在实现的时候,很容易一会按照行来计算一会按照列来计算,这样就会越写越乱。 @@ -65,7 +62,7 @@ 这句话可以有点绕,来举一个理解,例如求列4的雨水高度,如图: -![42.接雨水3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092732301.png) +![42.接雨水3](https://file.kamacoder.com/pics/20210223092732301.png) 列4 左侧最高的柱子是列3,高度为2(以下用lHeight表示)。 @@ -204,7 +201,7 @@ public: 1. 首先单调栈是按照行方向来计算雨水,如图: -![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092629946.png) +![42.接雨水2](https://file.kamacoder.com/pics/20210223092629946.png) 知道这一点,后面的就可以理解了。 @@ -218,7 +215,7 @@ public: 如图: -![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229.png) +![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229.png) 关于单调栈的顺序给大家一个总结: [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 中求一个元素右边第一个更大元素,单调栈就是递增的,[84.柱状图中最大的矩形](https://programmercarl.com/0084.柱状图中最大的矩形.html)求一个元素右边第一个更小元素,单调栈就是递减的。 @@ -232,7 +229,7 @@ public: 如图所示: -![42.接雨水5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223094619398.png) +![42.接雨水5](https://file.kamacoder.com/pics/20210223094619398.png) 4. 栈里要保存什么数值 @@ -287,7 +284,7 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况 如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示: -![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229-20230310123027977.png) +![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229-20230310123027977.png) 取栈顶元素,将栈顶元素弹出,这个就是凹槽的底部,也就是中间位置,下标记为mid,对应的高度为height[mid](就是图中的高度1)。 @@ -1095,7 +1092,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 8919d393..dd51384d 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心理准备! @@ -49,7 +47,7 @@ 如图: -![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103.png) +![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103.png) **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** @@ -101,11 +99,11 @@ public: 因为当移动下标指向 nums.size - 2 时: - 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即 ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图: - ![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png) + ![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286.png) - 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图: -![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693.png) +![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693.png) 代码如下: @@ -542,7 +540,3 @@ public class Solution ``` -

- - - diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index ca465efd..5a190242 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 46.全排列 @@ -43,7 +41,7 @@ 我以[1,2,3]为例,抽象成树形结构如下: -![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) +![全排列](https://file.kamacoder.com/pics/20240803180318.png) ### 回溯三部曲 @@ -55,7 +53,7 @@ 但排列问题需要一个used数组,标记已经选择的元素,如图橘黄色部分所示: -![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) +![全排列](https://file.kamacoder.com/pics/20240803180318.png) 代码如下: @@ -67,7 +65,7 @@ void backtracking (vector& nums, vector& used) * 递归终止条件 -![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) +![全排列](https://file.kamacoder.com/pics/20240803180318.png) 可以看出叶子节点,就是收割结果的地方。 @@ -521,8 +519,4 @@ public class Solution ``` -

- - - diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 08e3c616..6ed794aa 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -50,7 +48,7 @@ 我以示例中的 [1,1,2]为例 (为了方便举例,已经排序)抽象为一棵树,去重过程如图: -![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201331223.png) +![47.全排列II1](https://file.kamacoder.com/pics/20201124201331223.png) 图中我们对同一树层,前一位(也就是nums[i-1])如果使用过,那么就进行去重。 @@ -132,11 +130,11 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201406192.png) +![47.全排列II2](https://file.kamacoder.com/pics/20201124201406192.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201431571.png) +![47.全排列II3](https://file.kamacoder.com/pics/20201124201431571.png) 大家应该很清晰的看到,树层上对前一位去重非常彻底,效率很高,树枝上对前一位去重虽然最后可以得到答案,但是做了很多无用搜索。 @@ -555,7 +553,3 @@ public class Solution ``` -

- - - diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index 38fc07e7..2a90a023 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 51. N皇后 @@ -17,7 +15,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211020232201.png) +![](https://file.kamacoder.com/pics/20211020232201.png) * 输入:n = 4 * 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] @@ -47,7 +45,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 下面我用一个 3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303.jpg) +![51.N皇后](https://file.kamacoder.com/pics/20210130182532303.jpg) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -87,7 +85,7 @@ void backtracking(int n, int row, vector& chessboard) { * 递归终止条件 在如下树形结构中: -![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303-20230310122134167.jpg) +![51.N皇后](https://file.kamacoder.com/pics/20210130182532303-20230310122134167.jpg) 可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。 @@ -921,7 +919,3 @@ public class Solution ``` -

- - - diff --git a/problems/0052.N皇后II.md b/problems/0052.N皇后II.md index 271484a4..489ab1f7 100644 --- a/problems/0052.N皇后II.md +++ b/problems/0052.N皇后II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -15,7 +13,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并 上图为 8 皇后问题的一种解法。 -![51n皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20200821152118456.png) +![51n皇后](https://file.kamacoder.com/pics/20200821152118456.png) 给定一个整数 n,返回 n 皇后不同的解决方案的数量。 @@ -307,7 +305,3 @@ class Solution { } ``` -

- - - diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 705a00d7..6f8c2a6e 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 53. 最大子序和 @@ -492,7 +490,3 @@ public class Solution ``` -

- - - diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md index 38a3a118..174f55e8 100644 --- a/problems/0053.最大子序和(动态规划).md +++ b/problems/0053.最大子序和(动态规划).md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 53. 最大子序和 @@ -56,7 +54,7 @@ dp[0]应该是多少呢? 5. 举例推导dp数组 以示例一为例,输入:nums = [-2,1,-3,4,-1,2,1,-5,4],对应的dp状态如下: -![53.最大子序和(动态规划)](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303104129101.png) +![53.最大子序和(动态规划)](https://file.kamacoder.com/pics/20210303104129101.png) **注意最后的结果可不是dp[nums.size() - 1]!** ,而是dp[6]。 @@ -244,7 +242,3 @@ function maxSubArray(nums: number[]): number { -

- - - diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index 3b7afb90..a852b674 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -38,7 +36,7 @@ 由外向内一圈一圈这么画下去,如下所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png) +![](https://file.kamacoder.com/pics/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 @@ -485,7 +483,3 @@ func min(x, y int) int { ``` -

- - - diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 896dc998..0ebbcb59 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 55. 跳跃游戏 @@ -50,7 +48,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203105634.png) +![](https://file.kamacoder.com/pics/20230203105634.png) i 每次移动只能在 cover 的范围内移动,每移动一个元素,cover 得到该元素数值(新的覆盖范围)的补充,让 i 继续移动下去。 @@ -293,7 +291,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 538be693..cb06fcab 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 56. 合并区间 @@ -40,7 +38,7 @@ 这么说有点抽象,看图:(**注意图中区间都是按照左边界排序之后了**) -![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791.png) +![56.合并区间](https://file.kamacoder.com/pics/20201223200632791.png) 知道如何判断重复之后,剩下的就是合并了,如何去模拟合并区间呢? @@ -405,7 +403,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 94966126..d7aea257 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -56,7 +54,7 @@ 那么我按照左闭右开的原则,来画一圈,大家看一下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png) +![](https://file.kamacoder.com/pics/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 @@ -829,7 +827,3 @@ def generate_matrix(n) end ``` -

- - - diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 7025135a..20bd56ba 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -18,7 +16,7 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110174033215.png) +![](https://file.kamacoder.com/pics/20210110174033215.png) * 输入:m = 3, n = 7 * 输出:28 @@ -64,7 +62,7 @@ 如图举例: -![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700.png) +![62.不同路径](https://file.kamacoder.com/pics/20201209113602700.png) 此时问题就可以转化为求二叉树叶子节点的个数,代码如下: @@ -133,7 +131,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图所示: -![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392.png) +![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392.png) 以上动规五部曲分析完毕,C++代码如下: @@ -182,7 +180,7 @@ public: 在这个图中,可以看出一共m,n的话,无论怎么走,走到终点都需要 m + n - 2 步。 -![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700-20230310120944078.png) +![62.不同路径](https://file.kamacoder.com/pics/20201209113602700-20230310120944078.png) 在这m + n - 2 步中,一定有 m - 1 步是要向下走的,不用管什么时候向下走。 @@ -192,7 +190,7 @@ public: 那么答案,如图所示: -![62.不同路径2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113725324.png) +![62.不同路径2](https://file.kamacoder.com/pics/20201209113725324.png) **求组合的时候,要防止两个int相乘溢出!** 所以不能把算式的分子都算出来,分母都算出来再做除法。 @@ -616,7 +614,3 @@ public class Solution -

- - - diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 13923abe..d39036ba 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -16,13 +14,13 @@ 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204901338.png) +![](https://file.kamacoder.com/pics/20210111204901338.png) 网格中的障碍物和空位置分别用 1 和 0 来表示。 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204939971.png) +![](https://file.kamacoder.com/pics/20210111204939971.png) * 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] * 输出:2 @@ -34,7 +32,7 @@ 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111205857918.png) +![](https://file.kamacoder.com/pics/20210111205857918.png) * 输入:obstacleGrid = [[0,1],[0,0]] * 输出:1 @@ -95,7 +93,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图: -![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928.png) +![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928.png) 下标(0, j)的初始化情况同理。 @@ -129,11 +127,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983.png) +![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983.png) 对应的dp table 如图: -![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256.png) +![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256.png) 如果这个图看不懂,建议再理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下! @@ -781,7 +779,3 @@ public class Solution ``` -

- - - diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 6a13a21c..17bf3ee7 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 70. 爬楼梯 @@ -103,7 +101,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 举例当n为5的时候,dp table(dp数组)应该是这样的 -![70.爬楼梯](https://code-thinking-1253855093.file.myqcloud.com/pics/20210105202546299.png) +![70.爬楼梯](https://file.kamacoder.com/pics/20210105202546299.png) 如果代码出问题了,就把dp table 打印出来,看看究竟是不是和自己推导的一样。 @@ -520,8 +518,4 @@ impl Solution { ``` -

- - - diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index c51a590b..a5435ddd 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 70. 爬楼梯(进阶版) @@ -252,7 +250,3 @@ var climbStairs = function (n: number): number { ### Rust: -

- - - diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 0da3bf50..192ea470 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 72. 编辑距离 @@ -172,7 +170,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 可以看出dp[i][j]是依赖左方,上方和左上方元素的,如图: -![72.编辑距离](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162113131.jpg) +![72.编辑距离](https://file.kamacoder.com/pics/20210114162113131.jpg) 所以在dp矩阵中一定是从左到右从上到下去遍历。 @@ -196,7 +194,7 @@ for (int i = 1; i <= word1.size(); i++) { 以示例1为例,输入:`word1 = "horse", word2 = "ros"`为例,dp矩阵状态图如下: -![72.编辑距离1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162132300.jpg) +![72.编辑距离1](https://file.kamacoder.com/pics/20210114162132300.jpg) 以上动规五部分析完毕,C++代码如下: @@ -462,7 +460,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0077.组合.md b/problems/0077.组合.md index c4be5a38..c523c01c 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 第77题. 组合 @@ -84,7 +82,7 @@ for (int i = 1; i <= n; i++) { 那么我把组合问题抽象为如下树形结构: -![77.组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195223940.png) +![77.组合](https://file.kamacoder.com/pics/20201123195223940.png) 可以看出这棵树,一开始集合是 1,2,3,4, 从左向右取数,取过的数,不再重复取。 @@ -128,7 +126,7 @@ vector path; // 用来存放符合条件结果 从下图中红线部分可以看出,在集合[1,2,3,4]取1之后,下一层递归,就要在[2,3,4]中取数了,那么下一层递归如何知道从[2,3,4]中取数呢,靠的就是startIndex。 -![77.组合2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195328976.png) +![77.组合2](https://file.kamacoder.com/pics/20201123195328976.png) 所以需要startIndex来记录下一层递归,搜索的起始位置。 @@ -148,7 +146,7 @@ path这个数组的大小如果达到k,说明我们找到了一个子集大小 如图红色部分: -![77.组合3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195407907.png) +![77.组合3](https://file.kamacoder.com/pics/20201123195407907.png) 此时用result二维数组,把path保存起来,并终止本层递归。 @@ -165,7 +163,7 @@ if (path.size() == k) { 回溯法的搜索过程就是一个树型结构的遍历过程,在如下图中,可以看出for循环用来横向遍历,递归的过程是纵向遍历。 -![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195242899.png) +![77.组合1](https://file.kamacoder.com/pics/20201123195242899.png) 如此我们才遍历完图中的这棵树。 @@ -269,7 +267,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207-20230310134409532.png) +![77.组合4](https://file.kamacoder.com/pics/20210130194335207-20230310134409532.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 @@ -876,7 +874,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 0fa568af..c5e26e77 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -69,7 +67,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207.png) +![77.组合4](https://file.kamacoder.com/pics/20210130194335207.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 @@ -412,7 +410,3 @@ object Solution { ``` -

- - - diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 0c368b41..73eb385b 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 78.子集 @@ -492,7 +490,3 @@ public class Solution { -

- - - diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index 5c6f4073..e1a6671e 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 84.柱状图中最大的矩形 @@ -13,9 +11,9 @@ 求在该柱状图中,能够勾勒出来的矩形的最大面积。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210803220437.png) +![](https://file.kamacoder.com/pics/20210803220437.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210803220506.png) +![](https://file.kamacoder.com/pics/20210803220506.png) * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 @@ -116,7 +114,7 @@ public: 我来举一个例子,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221165730.png) +![](https://file.kamacoder.com/pics/20230221165730.png) 只有栈里从大到小的顺序,才能保证栈顶元素找到左右两边第一个小于栈顶元素的柱子。 @@ -181,7 +179,7 @@ public: 如果数组本身就是升序的,例如[2,4,6,8],那么入栈之后 都是单调递减,一直都没有走 情况三 计算结果的哪一步,所以最后输出的就是0了。 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221163936.png) +![](https://file.kamacoder.com/pics/20230221163936.png) 那么结尾加一个0,就会让栈里的所有元素,走到情况三的逻辑。 @@ -196,7 +194,7 @@ public: 之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 6 进行比较,周而复始,那么计算的最后结果result就是0。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221164533.png) +![](https://file.kamacoder.com/pics/20230221164533.png) 所以我们需要在 height数组前后各加一个元素0。 @@ -863,7 +861,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 811d3cc0..2f26e606 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 90.子集II @@ -41,7 +39,7 @@ 用示例中的[1, 2, 2] 来举例,如图所示: (**注意去重需要先对集合排序**) -![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124195411977.png) +![90.子集II](https://file.kamacoder.com/pics/20201124195411977.png) 从图中可以看出,同一树层上重复取2 就要过滤掉,同一树枝上就可以重复取2,因为同一树枝上元素的集合才是唯一子集! @@ -697,7 +695,3 @@ public class Solution ``` -

- - - diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index a03a0e3b..5ef21628 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -56,7 +54,7 @@ 切割问题可以抽象为树型结构,如图: -![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933.png) +![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933.png) ### 回溯三部曲 @@ -108,7 +106,7 @@ if (pointNum == 3) { // 逗点数量为3时,分隔结束 如果不合法就结束本层循环,如图中剪掉的分支: -![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310132314109.png) +![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310132314109.png) 然后就是递归和回溯的过程: @@ -876,7 +874,3 @@ public class Solution -

- - - diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index 25d79aff..ca99a466 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -14,7 +12,7 @@ 示例: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png) +![](https://file.kamacoder.com/pics/20210113161941835.png) ## 算法公开课 @@ -29,11 +27,11 @@ 了解了二叉搜索树之后,我们应该先举几个例子,画画图,看看有没有什么规律,如图: -![96.不同的二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093106367.png) +![96.不同的二叉搜索树](https://file.kamacoder.com/pics/20210107093106367.png) n为1的时候有一棵树,n为2有两棵树,这个是很直观的。 -![96.不同的二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093129889.png) +![96.不同的二叉搜索树1](https://file.kamacoder.com/pics/20210107093129889.png) 来看看n为3的时候,有哪几种情况。 @@ -67,7 +65,7 @@ dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索 如图所示: -![96.不同的二叉搜索树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093226241.png) +![96.不同的二叉搜索树2](https://file.kamacoder.com/pics/20210107093226241.png) 此时我们已经找到递推关系了,那么可以用动规五部曲再系统分析一遍。 @@ -120,7 +118,7 @@ for (int i = 1; i <= n; i++) { n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987.png) +![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987.png) 当然如果自己画图举例的话,基本举例到n为3就可以了,n为4的时候,画图已经比较麻烦了。 @@ -348,7 +346,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index fb4ca7d8..9569cbdd 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 98.验证二叉搜索树 @@ -18,7 +16,7 @@ * 节点的右子树只包含大于当前节点的数。 * 所有左子树和右子树自身必须也是二叉搜索树。 -![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png) +![98.验证二叉搜索树](https://file.kamacoder.com/pics/20230310000750.png) ## 算法公开课 @@ -104,7 +102,7 @@ if (root->val > root->left->val && root->val < root->right->val) { 例如: [10,5,15,null,null,6,20] 这个case: -![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000824.png) +![二叉搜索树](https://file.kamacoder.com/pics/20230310000824.png) 节点10大于左节点5,小于右节点15,但右子树里出现了一个6 这就不符合了! @@ -807,7 +805,3 @@ public bool IsValidBST(TreeNode root) ``` -

- - - diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index 7268b9f0..e5f61000 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -14,9 +12,9 @@ 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726172932.png) +![](https://file.kamacoder.com/pics/20210726172932.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) +![](https://file.kamacoder.com/pics/20210726173011.png) ## 思路 @@ -340,7 +338,3 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { -

- - - diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 31c24fc5..205597b0 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 101. 对称二叉树 @@ -11,7 +9,7 @@ 给定一个二叉树,检查它是否是镜像对称的。 -![101. 对称二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144607387.png) +![101. 对称二叉树](https://file.kamacoder.com/pics/20210203144607387.png) ## 算法公开课 @@ -27,7 +25,7 @@ 比较的是两个子树的里侧和外侧的元素是否相等。如图所示: -![101. 对称二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144624414.png) +![101. 对称二叉树1](https://file.kamacoder.com/pics/20210203144624414.png) 那么遍历的顺序应该是什么样的呢? @@ -946,8 +944,4 @@ public bool IsSymmetric(TreeNode root) ``` -

- - - diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index ce53e49a..6725d72c 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -39,7 +37,7 @@ 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 -![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png) +![102.二叉树的层序遍历](https://file.kamacoder.com/pics/20210203144842988.png) ### 思路 @@ -534,7 +532,7 @@ public IList> LevelOrder(TreeNode root) 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) -![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png) +![107.二叉树的层次遍历II](https://file.kamacoder.com/pics/20210203151058308.png) ### 思路 @@ -928,7 +926,7 @@ public IList> LevelOrderBottom(TreeNode root) 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 -![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png) +![199.二叉树的右视图](https://file.kamacoder.com/pics/20210203151307377.png) ### 思路 @@ -1278,7 +1276,7 @@ public class Solution 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 -![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png) +![637.二叉树的层平均值](https://file.kamacoder.com/pics/20210203151350500.png) ### 思路 @@ -1636,7 +1634,7 @@ public class Solution { 例如,给定一个 3叉树 : -![429. N叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151439168.png) +![429. N叉树的层序遍历](https://file.kamacoder.com/pics/20210203151439168.png) 返回其层序遍历: @@ -2008,7 +2006,7 @@ impl Solution { 您需要在二叉树的每一行中找到最大的值。 -![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png) +![515.在每个树行中找最大值](https://file.kamacoder.com/pics/20210203151532153.png) ### 思路 @@ -2339,7 +2337,7 @@ struct Node { 初始状态下,所有 next 指针都被设置为 NULL。 -![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg) +![116.填充每个节点的下一个右侧节点指针](https://file.kamacoder.com/pics/20210203152044855.jpg) ### 思路 @@ -2973,7 +2971,7 @@ object Solution { 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310134849764.png) +![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310134849764.png) 返回它的最大深度 3 。 @@ -2983,7 +2981,7 @@ object Solution { 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585-20230310134854803.png) +![层序遍历](https://file.kamacoder.com/pics/20200810193056585-20230310134854803.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 @@ -3601,7 +3599,3 @@ impl Solution { **致敬叶师傅!** -

- - - diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index fdc90096..2eb22ae5 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -20,7 +18,7 @@ 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310121809902.png) +![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310121809902.png) 返回它的最大深度 3 。 @@ -174,7 +172,7 @@ public: 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585.png) +![层序遍历](https://file.kamacoder.com/pics/20200810193056585.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 @@ -219,7 +217,7 @@ public: 例如,给定一个 3叉树 : -![559.n叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315313214.png) +![559.n叉树的最大深度](https://file.kamacoder.com/pics/2021020315313214.png) 我们应返回其最大深度,3。 @@ -1195,7 +1193,3 @@ public int MaxDepth(TreeNode root) -

- - - diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index bde61a75..2f8e5eef 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -27,7 +25,7 @@ * 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: -![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) +![106. 从中序与后序遍历序列构造二叉树1](https://file.kamacoder.com/pics/20210203154316774.png) ## 算法公开课 @@ -42,7 +40,7 @@ 流程如图: -![106.从中序与后序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154249860.png) +![106.从中序与后序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154249860.png) 那么代码应该怎么写呢? @@ -413,7 +411,7 @@ public: 中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: -![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) +![105. 从前序与中序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154626672.png) ### 思路 @@ -556,7 +554,7 @@ public: 举一个例子: -![106.从中序与后序遍历序列构造二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154720326.png) +![106.从中序与后序遍历序列构造二叉树2](https://file.kamacoder.com/pics/20210203154720326.png) tree1 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 @@ -1351,7 +1349,3 @@ public TreeNode BuildTree(int[] inorder, int[] postorder) ``` -

- - - diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 4804ccd3..5829e2d2 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 构造二叉搜索树,一不小心就平衡了 @@ -18,7 +16,7 @@ 示例: -![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png) +![108.将有序数组转换为二叉搜索树](https://file.kamacoder.com/pics/20201022164420763.png) ## 算法公开课 @@ -42,7 +40,7 @@ 例如 有序数组[-10,-3,0,5,9] 就可以构造成这样的二叉搜索树,如图。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220930173553.png) +![](https://file.kamacoder.com/pics/20220930173553.png) 上图中,是符合二叉搜索树的特性吧,如果要这么做的话,是不是本题意义就不大了,所以才强调是平衡二叉搜索树。 @@ -562,7 +560,3 @@ public TreeNode Traversal(int[] nums, int left, int right) -

- - - diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index a4339ac3..ff84ad84 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -21,7 +19,7 @@ 给定二叉树 [3,9,20,null,null,15,7] -![110.平衡二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315542230.png) +![110.平衡二叉树](https://file.kamacoder.com/pics/2021020315542230.png) 返回 true 。 @@ -29,7 +27,7 @@ 给定二叉树 [1,2,2,3,3,null,null,4,4] -![110.平衡二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155447919.png) +![110.平衡二叉树1](https://file.kamacoder.com/pics/20210203155447919.png) 返回 false 。 @@ -48,7 +46,7 @@ 但leetcode中强调的深度和高度很明显是按照节点来计算的,如图: -![110.平衡二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155515650.png) +![110.平衡二叉树2](https://file.kamacoder.com/pics/20210203155515650.png) 关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。 @@ -998,7 +996,3 @@ public int GetHeight(TreeNode root) ``` -

- - - diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 708e0532..bd4ea29d 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 和求最大深度一个套路? @@ -22,7 +20,7 @@ 给定二叉树 [3,9,20,null,null,15,7], -![111.二叉树的最小深度1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315582586.png) +![111.二叉树的最小深度1](https://file.kamacoder.com/pics/2021020315582586.png) 返回它的最小深度 2. @@ -752,7 +750,3 @@ public int MinDepth(TreeNode root) } ``` -

- - - diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index b97013e6..24891ace 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -17,7 +15,7 @@ 示例: 给定如下二叉树,以及目标和 sum = 22, -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230407210247.png) +![](https://file.kamacoder.com/pics/20230407210247.png) 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 @@ -55,7 +53,7 @@ 如图所示: -![112.路径总和](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020316051216.png) +![112.路径总和](https://file.kamacoder.com/pics/2021020316051216.png) 图中可以看出,遍历的路线,并不要遍历整棵树,所以递归函数需要返回值,可以用bool类型表示。 @@ -232,7 +230,7 @@ public: 给定如下二叉树,以及目标和 sum = 22, -![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) +![113.路径总和ii1.png](https://file.kamacoder.com/pics/20210203160854654.png) ### 思路 @@ -241,7 +239,7 @@ public: 如图: -![113.路径总和ii](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160922745.png) +![113.路径总和ii](https://file.kamacoder.com/pics/20210203160922745.png) 为了尽可能的把细节体现出来,我写出如下代码(**这份代码并不简洁,但是逻辑非常清晰**) @@ -309,25 +307,25 @@ public: 0112.路径总和 ```java -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if (root == null) { return false; } - targetsum -= root.val; + targetSum -= root.val; // 叶子结点 if (root.left == null && root.right == null) { - return targetsum == 0; + return targetSum == 0; } if (root.left != null) { - boolean left = haspathsum(root.left, targetsum); - if (left) { // 已经找到 + boolean left = hasPathSum(root.left, targetSum); + if (left) { // 已经找到,提前返回 return true; } } if (root.right != null) { - boolean right = haspathsum(root.right, targetsum); - if (right) { // 已经找到 + boolean right = hasPathSum(root.right, targetSum); + if (right) { // 已经找到,提前返回 return true; } } @@ -336,16 +334,16 @@ class solution { } // lc112 简洁方法 -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if (root == null) return false; // 为空退出 // 叶子节点判断是否符合 - if (root.left == null && root.right == null) return root.val == targetsum; + if (root.left == null && root.right == null) return root.val == targetSum; // 求两侧分支的路径和 - return haspathsum(root.left, targetsum - root.val) || haspathsum(root.right, targetsum - root.val); + return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } } ``` @@ -353,22 +351,22 @@ class solution { 迭代 ```java -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if(root == null) return false; - stack stack1 = new stack<>(); - stack stack2 = new stack<>(); + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); stack1.push(root); stack2.push(root.val); - while(!stack1.isempty()) { + while(!stack1.isEmpty()) { int size = stack1.size(); for(int i = 0; i < size; i++) { - treenode node = stack1.pop(); + TreeNode node = stack1.pop(); int sum = stack2.pop(); // 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true - if(node.left == null && node.right == null && sum == targetsum) { + if(node.left == null && node.right == null && sum == targetSum) { return true; } // 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来 @@ -387,8 +385,9 @@ class solution { } } ``` -```Java 統一迭代法 - public boolean hasPathSum(TreeNode root, int targetSum) { +```Java +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { Stack treeNodeStack = new Stack<>(); Stack sumStack = new Stack<>(); @@ -422,38 +421,39 @@ class solution { } return false; } +} ``` 0113.路径总和-ii ```java -class solution { - public List> pathsum(TreeNode root, int targetsum) { +class Solution { + public List> pathSum(TreeNode root, int targetSum) { List> res = new ArrayList<>(); if (root == null) return res; // 非空判断 List path = new LinkedList<>(); - preorderdfs(root, targetsum, res, path); + preOrderDfs(root, targetSum, res, path); return res; } - public void preorderdfs(TreeNode root, int targetsum, List> res, List path) { + public void preOrderDfs(TreeNode root, int targetSum, List> res, List path) { path.add(root.val); // 遇到了叶子节点 if (root.left == null && root.right == null) { // 找到了和为 targetsum 的路径 - if (targetsum - root.val == 0) { + if (targetSum - root.val == 0) { res.add(new ArrayList<>(path)); } return; // 如果和不为 targetsum,返回 } if (root.left != null) { - preorderdfs(root.left, targetsum - root.val, res, path); + preOrderDfs(root.left, targetSum - root.val, res, path); path.remove(path.size() - 1); // 回溯 } if (root.right != null) { - preorderdfs(root.right, targetsum - root.val, res, path); + preOrderDfs(root.right, targetSum - root.val, res, path); path.remove(path.size() - 1); // 回溯 } } @@ -1622,7 +1622,4 @@ public class Solution { ``` -

- - - + diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index cf24c4c1..1df3d899 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 115.不同的子序列 @@ -72,7 +70,7 @@ dp[i][j]:以i-1为结尾的s子序列中出现以j-1为结尾的t的个数为d 从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j] 是从上方和左上方推导而来,如图:,那么 dp[i][0] 和dp[0][j]是一定要初始化的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221222165412.png) +![](https://file.kamacoder.com/pics/20221222165412.png) 每次当初始化的时候,都要回顾一下dp[i][j]的定义,不要凭感觉初始化。 @@ -103,7 +101,7 @@ for (int j = 1; j <= t.size(); j++) dp[0][j] = 0; // 其实这行代码可以和 从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j]都是根据左上方和正上方推出来的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221222165412.png) +![](https://file.kamacoder.com/pics/20221222165412.png) 所以遍历的时候一定是从上到下,从左到右,这样保证dp[i][j]可以根据之前计算出来的数值进行计算。 @@ -376,7 +374,3 @@ impl Solution { -

- - - diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 98bd4e41..9de1de1e 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 116. 填充每个节点的下一个右侧节点指针 @@ -28,7 +26,7 @@ struct Node { * 你只能使用常量级额外空间。 * 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) +![](https://file.kamacoder.com/pics/20210727143202.png) ## 思路 @@ -489,8 +487,4 @@ public class Solution -

- - - diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index f8092503..f82ed962 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 121. 买卖股票的最佳时机 @@ -131,7 +129,7 @@ dp[0][1]表示第0天不持有股票,不持有股票那么现金就是0,所 以示例1,输入:[7,1,5,3,6,4]为例,dp数组状态如下: -![121.买卖股票的最佳时机](https://code-thinking-1253855093.file.myqcloud.com/pics/20210224225642465.png) +![121.买卖股票的最佳时机](https://file.kamacoder.com/pics/20210224225642465.png) dp[5][1]就是最终结果。 @@ -627,7 +625,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 6663a66d..4ccb17bb 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 122.买卖股票的最佳时机 II @@ -68,7 +66,7 @@ 如图: -![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858-20230310134659477.png) +![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858-20230310134659477.png) 一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。 @@ -423,7 +421,3 @@ public class Solution ``` -

- - - diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 0dced9ef..d8cb308b 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 122.买卖股票的最佳时机II @@ -477,7 +475,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 1b7c09d2..c4ff89a0 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 123.买卖股票的最佳时机III @@ -122,7 +120,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295-20230310134201291.png) +![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295-20230310134201291.png) 大家可以看到红色框为最后两次卖出的状态。 @@ -565,7 +563,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 00d7d4cf..1ce0bc11 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 127. 单词接龙 @@ -33,7 +31,7 @@ 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210827175432.png) +![](https://file.kamacoder.com/pics/20210827175432.png) 本题只需要求出最短路径的长度就可以了,不用找出路径。 @@ -360,7 +358,3 @@ function diffonechar(word1: string, word2: string): boolean { ``` -

- - - diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index 90dfd061..923bc638 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -383,7 +381,3 @@ int sumNumbers(struct TreeNode* root){ ``` -

- - - diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index 8ef8d5b2..278c12ec 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 130. 被围绕的区域 @@ -10,7 +8,7 @@ 给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220901104745.png) +![](https://file.kamacoder.com/pics/20220901104745.png) * 输入:board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]] * 输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]] @@ -30,11 +28,11 @@ 步骤一:深搜或者广搜将地图周边的'O'全部改成'A',如图所示: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902102337.png) +![图一](https://file.kamacoder.com/pics/20220902102337.png) 步骤二:在遍历地图,将'O'全部改成'X'(地图中间的'O'改成了'X'),将'A'改回'O'(保留的地图周边的'O'),如图所示: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902102831.png) +![图二](https://file.kamacoder.com/pics/20220902102831.png) 整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 @@ -793,7 +791,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 4eca0ddf..f9b5d244 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 切割问题其实是一种组合问题! @@ -1007,7 +1005,3 @@ public class Solution -

- - - diff --git a/problems/0132.分割回文串II.md b/problems/0132.分割回文串II.md index 85e047f2..089dd52c 100644 --- a/problems/0132.分割回文串II.md +++ b/problems/0132.分割回文串II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -163,7 +161,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 以输入:"aabc" 为例: -![132.分割回文串II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210124182218844.jpg) +![132.分割回文串II](https://file.kamacoder.com/pics/20210124182218844.jpg) 以上分析完毕,代码如下: @@ -373,7 +371,3 @@ var minCut = function(s) { -

- - - diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 0248760d..354f6424 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 134. 加油站 @@ -146,7 +144,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230117165628.png) +![](https://file.kamacoder.com/pics/20230117165628.png) 那么为什么一旦[0,i] 区间和为负数,起始位置就可以是i+1呢,i+1后面就不会出现更大的负数? @@ -154,7 +152,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 那有没有可能 [0,i] 区间 选某一个作为起点,累加到 i这里 curSum是不会小于零呢? 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230117170703.png) +![](https://file.kamacoder.com/pics/20230117170703.png) 如果 curSum<0 说明 区间和1 + 区间和2 < 0, 那么 假设从上图中的位置开始计数curSum不会小于0的话,就是 区间和2>0。 @@ -709,7 +707,3 @@ public class Solution ``` -

- - - diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index eb2081fe..30df2149 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 135. 分发糖果 @@ -58,7 +56,7 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878.png) +![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878.png) 再确定左孩子大于右孩子的情况(从后向前遍历) @@ -68,7 +66,7 @@ for (int i = 1; i < ratings.size(); i++) { 如果从前向后遍历,rating[5]与rating[4]的比较 就不能用上 rating[5]与rating[6]的比较结果了 。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230202102044.png) +![](https://file.kamacoder.com/pics/20230202102044.png) **所以确定左孩子大于右孩子的情况一定要从后向前遍历!** @@ -84,7 +82,7 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791.png) +![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791.png) 所以该过程代码如下: @@ -401,7 +399,3 @@ public class Solution -

- - - diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 29748e27..513d327b 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -182,7 +180,7 @@ dp[0]表示如果字符串为空的话,说明出现在字典里。 以输入: s = "leetcode", wordDict = ["leet", "code"]为例,dp状态如图: -![139.单词拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202162652727.jpg) +![139.单词拆分](https://file.kamacoder.com/pics/20210202162652727.jpg) dp[s.size()]就是最终结果。 @@ -243,7 +241,7 @@ public: 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240809155103.png) +![](https://file.kamacoder.com/pics/20240809155103.png) 最后dp[s.size()] = 0 即 dp[13] = 0 ,而不是1,因为先用 "apple" 去遍历的时候,dp[8]并没有被赋值为1 (还没用"pen"),所以 dp[13]也不能变成1。 @@ -565,7 +563,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md index ac656576..685a92d5 100644 --- a/problems/0141.环形链表.md +++ b/problems/0141.环形链表.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 141. 环形链表 @@ -15,7 +13,7 @@ 如果链表中存在环,则返回 true 。 否则,返回 false 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727173600.png) +![](https://file.kamacoder.com/pics/20210727173600.png) ## 思路 @@ -160,7 +158,3 @@ function hasCycle(head: ListNode | null): boolean { -

- - - diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index 7cda58c3..6cfabc60 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -22,7 +20,7 @@ **说明**:不允许修改给定的链表。 -![循环链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20200816110112704.png) +![循环链表](https://file.kamacoder.com/pics/20200816110112704.png) ## 算法公开课 @@ -52,7 +50,7 @@ 会发现最终都是这种情况, 如下图: -![142环形链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318162236720.png) +![142环形链表1](https://file.kamacoder.com/pics/20210318162236720.png) fast和slow各自再走一步, fast和slow就相遇了 @@ -72,7 +70,7 @@ fast和slow各自再走一步, fast和slow就相遇了 环形入口节点到 fast指针与slow指针相遇节点 节点数为y。 从相遇节点 再到环形入口节点节点数为 z。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220925103433.png) +![](https://file.kamacoder.com/pics/20220925103433.png) 那么相遇时: slow指针走过的节点数为: `x + y`, @@ -156,20 +154,20 @@ public: 即文章[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中如下的地方: -![142环形链表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318165123581.png) +![142环形链表5](https://file.kamacoder.com/pics/20210318165123581.png) 首先slow进环的时候,fast一定是先进环来了。 如果slow进环入口,fast也在环入口,那么把这个环展开成直线,就是如下图的样子: -![142环形链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816503266.png) +![142环形链表3](https://file.kamacoder.com/pics/2021031816503266.png) 可以看出如果slow 和 fast同时在环入口开始走,一定会在环入口3相遇,slow走了一圈,fast走了两圈。 重点来了,slow进环的时候,fast一定是在环的任意一个位置,如图: -![142环形链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816515727.png) +![142环形链表4](https://file.kamacoder.com/pics/2021031816515727.png) 那么fast指针走到环入口3的时候,已经走了k + n 个节点,slow相应的应该走了(k + n) / 2 个节点。 @@ -465,7 +463,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index ccddef5b..98488bc1 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -1,14 +1,12 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 143.重排链表 [力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726160122.png) +![](https://file.kamacoder.com/pics/20210726160122.png) ## 思路 @@ -689,7 +687,3 @@ void reorderList(struct ListNode* head){ ``` -

- - - diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index bc73f6da..6d21452d 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 这不仅仅是一道好题,也展现出计算机的思考方式 @@ -550,7 +548,3 @@ int evalRPN(char** tokens, int tokensSize) { } ``` -

- - - diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 3dbd59b9..b5246a7d 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1086,8 +1084,4 @@ public string ReverseWords(string s) { } ``` -

- - - diff --git a/problems/0160.相交链表.md b/problems/0160.相交链表.md index d4422bd8..cdc58912 100644 --- a/problems/0160.相交链表.md +++ b/problems/0160.相交链表.md @@ -1,11 +1,5 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) 同:[链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html) -

- - - diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index cbba12c9..a3fc7ef1 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 188.买卖股票的最佳时机IV @@ -134,7 +132,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221.png) +![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 @@ -641,8 +639,4 @@ impl Solution { -

- - - diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index e91109c6..976cbed4 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 189. 旋转数组 @@ -212,7 +210,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index 032204bb..0bee40f7 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 198.打家劫舍 @@ -89,7 +87,7 @@ for (int i = 2; i < nums.size(); i++) { 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115.jpg) +![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115.jpg) 红框dp[nums.size() - 1]为结果。 @@ -361,7 +359,3 @@ impl Solution { -

- - - diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md index 00e4efd8..9ea47329 100644 --- a/problems/0200.岛屿数量.广搜版.md +++ b/problems/0200.岛屿数量.广搜版.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 200. 岛屿数量 @@ -15,7 +13,7 @@ 此外,你可以假设该网格的四条边均被水包围。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726093256.png) +![](https://file.kamacoder.com/pics/20220726093256.png) 提示: @@ -30,7 +28,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -50,7 +48,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png) +![图二](https://file.kamacoder.com/pics/20220727100846.png) 超时写法 (从队列中取出节点再标记) @@ -410,7 +408,3 @@ impl Solution { ``` ``` -

- - - diff --git a/problems/0200.岛屿数量.深搜版.md b/problems/0200.岛屿数量.深搜版.md index 46579203..a0153998 100644 --- a/problems/0200.岛屿数量.深搜版.md +++ b/problems/0200.岛屿数量.深搜版.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 200. 岛屿数量 @@ -14,7 +12,7 @@ 此外,你可以假设该网格的四条边均被水包围。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726093256.png) +![](https://file.kamacoder.com/pics/20220726093256.png) 提示: @@ -29,7 +27,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -463,7 +461,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 39cb39fa..fdcadee9 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -558,7 +556,3 @@ def next_value(n) end ``` -

- - - diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 5a4bbb74..5ecf89bf 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -36,11 +34,11 @@ 这里以链表 1 4 2 4 来举例,移除元素4。 -![203_链表删除元素1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095351161.png) +![203_链表删除元素1](https://file.kamacoder.com/pics/20210316095351161.png) 如果使用C,C++编程语言的话,不要忘了还要从内存中删除这两个移除的节点, 清理节点内存之后如图: -![203_链表删除元素2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095418280.png) +![203_链表删除元素2](https://file.kamacoder.com/pics/20210316095418280.png) **当然如果使用java ,python的话就不用手动管理内存了。** @@ -58,16 +56,16 @@ 来看第一种操作:直接使用原来的链表来进行移除。 -![203_链表删除元素3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031609544922.png) +![203_链表删除元素3](https://file.kamacoder.com/pics/2021031609544922.png) 移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。 所以头结点如何移除呢,其实只要将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。 -![203_链表删除元素4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095512470.png) +![203_链表删除元素4](https://file.kamacoder.com/pics/20210316095512470.png) 依然别忘将原头结点从内存中删掉。 -![203_链表删除元素5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095543775.png) +![203_链表删除元素5](https://file.kamacoder.com/pics/20210316095543775.png) 这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。 @@ -78,7 +76,7 @@ 来看看如何设置一个虚拟头。依然还是在这个链表中,移除元素1。 -![203_链表删除元素6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095619221.png) +![203_链表删除元素6](https://file.kamacoder.com/pics/20210316095619221.png) 这里来给链表添加一个虚拟头结点为新的头结点,此时要移除这个旧头结点元素1。 @@ -808,7 +806,3 @@ end ``` -

- - - diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index e416d9ce..ba255e06 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 205. 同构字符串 @@ -180,7 +178,3 @@ function isIsomorphic(s: string, t: string): boolean { -

- - - diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 430bebe5..4e33342a 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 @@ -29,7 +27,7 @@ 其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: -![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) +![206_反转链表](https://file.kamacoder.com/pics/20210218090901207.png) 之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 @@ -739,7 +737,3 @@ public ListNode reverseList(ListNode head) { > 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` ![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) -

- - - diff --git a/problems/0207.课程表.md b/problems/0207.课程表.md index dff0b18e..f992c72b 100644 --- a/problems/0207.课程表.md +++ b/problems/0207.课程表.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) 拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是 广搜 可能是深搜。 @@ -59,7 +57,3 @@ public: } }; ``` -

- - - diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index c6d89976..ca24bc42 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 209.长度最小的子数组 @@ -106,7 +104,7 @@ public: 解题的关键在于 窗口的起始位置如何移动,如图所示: -![leetcode_209](https://code-thinking-1253855093.file.myqcloud.com/pics/20210312160441942.png) +![leetcode_209](https://file.kamacoder.com/pics/20210312160441942.png) 可以发现**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)暴力解法降为O(n)。** @@ -558,7 +556,3 @@ public class Solution { } } ``` -

- - - diff --git a/problems/0210.课程表II.md b/problems/0210.课程表II.md index 2d2e2429..b0d9fe8a 100644 --- a/problems/0210.课程表II.md +++ b/problems/0210.课程表II.md @@ -1,3 +1,6 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) ```CPP class Solution { diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index 05ebd1ad..8fceb0a9 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 213.打家劫舍II @@ -44,15 +42,15 @@ * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643-20230310134000692.jpg) +![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643-20230310134000692.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374-20230310134003961.jpg) +![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374-20230310134003961.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491-20230310134008133.jpg) +![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491-20230310134008133.jpg) **注意我这里用的是"考虑"**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素! 对于情况三,取nums[1] 和 nums[3]就是最大的。 @@ -367,7 +365,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 3d7f2d0c..e23be78d 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -47,7 +45,7 @@ 选取过程如图: -![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975.png) +![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975.png) 图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。 @@ -110,7 +108,7 @@ if (path.size() == k) { 本题和[77. 组合](https://programmercarl.com/0077.组合.html)区别之一就是集合固定的就是9个数[1,...,9],所以for循环固定i<=9 如图: -![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975-20230310113546003.png) +![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975-20230310113546003.png) 处理过程就是 path收集每次选取的元素,相当于树型结构里的边,sum来统计path里元素的总和。 @@ -168,7 +166,7 @@ public: 这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。 如图: -![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112319580476.png) +![216.组合总和III1](https://file.kamacoder.com/pics/2020112319580476.png) 已选元素总和如果已经大于n(图中数值为4)了,那么往后遍历就没有意义了,直接剪掉。 @@ -739,7 +737,3 @@ public class Solution -

- - - diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 8d7779f9..37ae7819 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 222.完全二叉树的节点个数 @@ -155,7 +153,7 @@ public: 我来举一个典型的例子如题: - + 完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。 @@ -164,10 +162,10 @@ public: 对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。 完全二叉树(一)如图: -![222.完全二叉树的节点个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092543662.png) +![222.完全二叉树的节点个数](https://file.kamacoder.com/pics/20201124092543662.png) 完全二叉树(二)如图: -![222.完全二叉树的节点个数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092634138.png) +![222.完全二叉树的节点个数1](https://file.kamacoder.com/pics/20201124092634138.png) 可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。 @@ -175,15 +173,15 @@ public: 在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163554.png) +![](https://file.kamacoder.com/pics/20220829163554.png) 在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163709.png) +![](https://file.kamacoder.com/pics/20220829163709.png) 那有录友说了,这种情况,递归向左遍历的深度等于递归向右遍历的深度,但也不是满二叉树,如题: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163811.png) +![](https://file.kamacoder.com/pics/20220829163811.png) 如果这么想,大家就是对 完全二叉树理解有误区了,**以上这棵二叉树,它根本就不是一个完全二叉树**! @@ -894,7 +892,3 @@ public int CountNodes(TreeNode root) ``` -

- - - diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 73d9db1b..23968580 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1367,7 +1365,3 @@ void myStackFree(MyStack* obj) { ``` -

- - - diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index c34ca4bf..248a28a4 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 226.翻转二叉树 @@ -12,7 +10,7 @@ 翻转一棵二叉树。 -![226.翻转二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192644329.png) +![226.翻转二叉树](https://file.kamacoder.com/pics/20210203192644329.png) 这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,全当一个乐子哈) @@ -37,7 +35,7 @@ 如果要从整个树来看,翻转还真的挺复杂,整个树以中间分割线进行翻转,如图: -![226.翻转二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192724351.png) +![226.翻转二叉树1](https://file.kamacoder.com/pics/20210203192724351.png) 可以发现想要翻转它,其实就把每一个节点的左右孩子交换一下就可以了。 @@ -1022,7 +1020,3 @@ public TreeNode InvertTree(TreeNode root) { ``` -

- - - diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 657567cf..6775a372 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 工作上一定没人这么搞,但是考察对栈、队列理解程度的好题 @@ -691,7 +689,3 @@ impl MyQueue { } ``` -

- - - diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index 1356b7da..f4933839 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 234.回文链表 @@ -429,7 +427,3 @@ function reverseList(head: ListNode | null): ListNode | null { -

- - - diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 3911261a..98cc5b7d 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 235. 二叉搜索树的最近公共祖先 @@ -16,7 +14,7 @@ 例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5] -![235. 二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201018172243602.png) +![235. 二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20201018172243602.png) 示例 1: @@ -54,7 +52,7 @@ 如图,我们从根节点搜索,第一次遇到 cur节点是数值在[q, p]区间中,即 节点5,此时可以说明 q 和 p 一定分别存在于 节点 5的左子树,和右子树中。 -![235.二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926164214.png) +![235.二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20220926164214.png) 此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为p的祖先, 如果从节点5继续向右遍历则错过成为q的祖先。 @@ -66,7 +64,7 @@ 如图所示:p为节点6,q为节点9 -![235.二叉搜索树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926165141.png) +![235.二叉搜索树的最近公共祖先2](https://file.kamacoder.com/pics/20220926165141.png) 可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! @@ -548,7 +546,3 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) -

- - - diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 8cd505a8..537d6240 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 本来是打算将二叉树和二叉搜索树的公共祖先问题一起讲,后来发现篇幅过长了,只能先说一说二叉树的公共祖先问题。 @@ -18,7 +16,7 @@ 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4] -![236. 二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201016173414722.png) +![236. 二叉树的最近公共祖先](https://file.kamacoder.com/pics/20201016173414722.png) 示例 1: 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 @@ -53,7 +51,7 @@ **首先最容易想到的一个情况:如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。** 即情况一: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922173502.png) +![](https://file.kamacoder.com/pics/20220922173502.png) 判断逻辑是 如果递归遍历遇到q,就将q返回,遇到p 就将p返回,那么如果 左右子树的返回值都不为空,说明此时的中节点,一定是q 和p 的最近祖先。 @@ -63,7 +61,7 @@ **但是很多人容易忽略一个情况,就是节点本身p(q),它拥有一个子孙节点q(p)。** 情况二: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922173530.png) +![](https://file.kamacoder.com/pics/20220922173530.png) 其实情况一 和 情况二 代码实现过程都是一样的,也可以说,实现情况一的逻辑,顺便包含了情况二。 @@ -131,7 +129,7 @@ left与right的逻辑处理; // 中 如图: -![236.二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415105872.png) +![236.二叉树的最近公共祖先](https://file.kamacoder.com/pics/2021020415105872.png) 就像图中一样直接返回7。 @@ -164,7 +162,7 @@ TreeNode* right = lowestCommonAncestor(root->right, p, q); 如图: -![236.二叉树的最近公共祖先1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151125844.png) +![236.二叉树的最近公共祖先1](https://file.kamacoder.com/pics/20210204151125844.png) 图中节点10的左子树返回null,右子树返回目标值7,那么此时节点10的处理逻辑就是把右子树的返回值(最近公共祖先7)返回上去! @@ -185,7 +183,7 @@ else { // (left == NULL && right == NULL) 那么寻找最小公共祖先,完整流程图如下: -![236.二叉树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/202102041512582.png) +![236.二叉树的最近公共祖先2](https://file.kamacoder.com/pics/202102041512582.png) **从图中,大家可以看到,我们是如何回溯遍历整棵二叉树,将结果返回给头结点的!** @@ -491,7 +489,3 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) ``` -

- - - diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 651e4da4..875f1bd1 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -922,8 +920,4 @@ int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) { ``` -

- - - diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 61488f03..9a783e5b 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 数组就是简单的哈希表,但是数组的大小可不是无限开辟的 @@ -416,7 +414,3 @@ bool isAnagram(char* s, char* t) { -

- - - diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 2d9292bc..5d713511 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 以为只用了递归,其实还用了回溯 @@ -16,7 +14,7 @@ 说明: 叶子节点是指没有子节点的节点。 示例: -![257.二叉树的所有路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415161576.png) +![257.二叉树的所有路径1](https://file.kamacoder.com/pics/2021020415161576.png) ## 算法公开课 @@ -30,7 +28,7 @@ 前序遍历以及回溯的过程如图: -![257.二叉树的所有路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151702443.png) +![257.二叉树的所有路径](https://file.kamacoder.com/pics/20210204151702443.png) 我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。** @@ -69,15 +67,15 @@ if (cur->left == NULL && cur->right == NULL) { 再来看一下终止处理的逻辑。 -这里使用vector 结构path来记录路径,所以要把vector 结构的path转为string格式,再把这个string 放进 result里。 +这里使用`vector` 结构path来记录路径,所以要把`vector` 结构的path转为string格式,再把这个string 放进 result里。 -**那么为什么使用了vector 结构来记录路径呢?** 因为在下面处理单层递归逻辑的时候,要做回溯,使用vector方便来做回溯。 +**那么为什么使用了`vector` 结构来记录路径呢?** 因为在下面处理单层递归逻辑的时候,要做回溯,使用vector方便来做回溯。 可能有的同学问了,我看有些人的代码也没有回溯啊。 **其实是有回溯的,只不过隐藏在函数调用时的参数赋值里**,下文我还会提到。 -这里我们先使用vector结构的path容器来记录路径,那么终止处理逻辑如下: +这里我们先使用`vector`结构的path容器来记录路径,那么终止处理逻辑如下: ```CPP if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点 @@ -317,7 +315,7 @@ public: 其实关键还在于 参数,使用的是 `string path`,这里并没有加上引用`&` ,即本层递归中,path + 该节点数值,但该层递归结束,上一层path的数值并不会受到任何影响。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220831173322.png) +![](https://file.kamacoder.com/pics/20220831173322.png) 节点4 的path,在遍历到节点3,path+3,遍历节点3的递归结束之后,返回节点4(回溯的过程),path并不会把3加上。 @@ -938,7 +936,3 @@ public void Traversal(TreeNode node, List path, List res) } ``` -

- - - diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index dc5a7e9e..8171a409 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 279.完全平方数 @@ -95,7 +93,7 @@ for (int i = 0; i <= n; i++) { // 遍历背包 已输入n为5例,dp状态图如下: -![279.完全平方数](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202112617341.jpg) +![279.完全平方数](https://file.kamacoder.com/pics/20210202112617341.jpg) dp[0] = 0 dp[1] = min(dp[0] + 1) = 1 @@ -479,7 +477,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index cbce0295..d7911054 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 283. 移动零:动态规划:一样的套路,再求一次完全平方数 @@ -186,7 +184,3 @@ impl Solution { -

- - - diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 442938c0..de37ed5c 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 300.最长递增子序列 @@ -87,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) { 输入:[0,1,0,3,2],dp数组的变化如下: -![300.最长上升子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110170945618.jpg) +![300.最长上升子序列](https://file.kamacoder.com/pics/20210110170945618.jpg) 如果代码写出来,但一直AC不了,那么就把dp数组打印出来,看看对不对! @@ -361,7 +359,3 @@ func lengthOfLIS(nums: Array): Int64 { ``` -

- - - diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index b98a416c..599a1f42 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 309.最佳买卖股票时机含冷冻期 @@ -49,7 +47,7 @@ dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]。 * 状态三:今天卖出股票 * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! -![](https://code-thinking-1253855093.file.myqcloud.com/pics/518d5baaf33f4b2698064f8efb42edbf.png) +![](https://file.kamacoder.com/pics/518d5baaf33f4b2698064f8efb42edbf.png) j的状态为: @@ -138,7 +136,7 @@ dp[i][3] = dp[i - 1][2]; 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032317451040.png) +![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/2021032317451040.png) 最后结果是取 状态二,状态三,和状态四的最大值,不少同学会把状态四忘了,状态四是冷冻期,最后一天如果是冷冻期也可能是最大值。 @@ -603,8 +601,4 @@ impl Solution { ``` -

- - - diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index e55e20be..7f3bc1e4 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 322. 零钱兑换 @@ -106,7 +104,7 @@ dp[0] = 0; 以输入:coins = [1, 2, 5], amount = 5为例 -![322.零钱兑换](https://code-thinking-1253855093.file.myqcloud.com/pics/20210201111833906.jpg) +![322.零钱兑换](https://file.kamacoder.com/pics/20210201111833906.jpg) dp[amount]为最终结果。 @@ -499,7 +497,3 @@ function coinChange(coins: number[], amount: number): number { ``` -

- - - diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 78e14074..fcdb6a33 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 这也可以用回溯法? 其实深搜和回溯也是相辅相成的,毕竟都用递归。 @@ -59,7 +57,7 @@ 对于死循环,我来举一个有重复机场的例子: -![332.重新安排行程](https://code-thinking-1253855093.file.myqcloud.com/pics/20201115180537865.png) +![332.重新安排行程](https://file.kamacoder.com/pics/20201115180537865.png) 为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。** @@ -113,7 +111,7 @@ void backtracking(参数) { 本题以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![332.重新安排行程1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555-20230310121223600.png) +![332.重新安排行程1](https://file.kamacoder.com/pics/2020111518065555-20230310121223600.png) 开始回溯三部曲讲解: @@ -139,7 +137,7 @@ bool backtracking(int ticketNum, vector& result) { 因为我们只需要找到一个行程,就是在树形结构中唯一的一条通向叶子节点的路线,如图: -![332.重新安排行程1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555-20230310121240991.png) +![332.重新安排行程1](https://file.kamacoder.com/pics/2020111518065555-20230310121240991.png) 所以找到了这个叶子节点了直接返回,这个递归函数的返回值问题我们在讲解二叉树的系列的时候,在这篇[二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](https://programmercarl.com/0112.路径总和.html)详细介绍过。 @@ -941,7 +939,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index a3130df7..4916af4c 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 337.打家劫舍 III @@ -14,7 +12,7 @@ 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 -![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) +![337.打家劫舍III](https://file.kamacoder.com/pics/20210223173849619.png) ## 算法公开课 @@ -179,7 +177,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203110031.png) +![](https://file.kamacoder.com/pics/20230203110031.png) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 @@ -623,7 +621,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 5d0110f6..203c4228 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 343. 整数拆分 @@ -129,7 +127,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104173021581.png) +![343.整数拆分](https://file.kamacoder.com/pics/20210104173021581.png) 以上动规五部曲分析完毕,C++代码如下: @@ -564,7 +562,3 @@ public class Solution ``` -

- - - diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 793c9af3..c88d008c 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -429,7 +427,3 @@ object Solution { } } ``` -

- - - diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index cca9b0ed..b6575c5f 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 前K个大数问题,老生常谈,不得不谈 @@ -609,8 +607,4 @@ impl Solution { ``` -

- - - diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 93fa0931..65d22a80 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -16,7 +14,7 @@ 题意:给定两个数组,编写一个函数来计算它们的交集。 -![349. 两个数组的交集](https://code-thinking-1253855093.file.myqcloud.com/pics/20200818193523911.png) +![349. 两个数组的交集](https://file.kamacoder.com/pics/20200818193523911.png) **说明:** 输出结果中的每个元素一定是唯一的。 @@ -53,7 +51,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 思路如图所示: -![set哈希法](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707173513.png) +![set哈希法](https://file.kamacoder.com/pics/20220707173513.png) C++代码如下: @@ -544,8 +542,4 @@ end * [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/) -

- - - diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index e2ea9904..50934981 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 376. 摆动序列 @@ -48,7 +46,7 @@ 用示例二来举例,如图所示: -![376.摆动序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174327597.png) +![376.摆动序列](https://file.kamacoder.com/pics/20201124174327597.png) **局部最优:删除单调坡度上的节点(不包括单调坡度两端的节点),那么这个坡度就可以有两个局部峰值**。 @@ -74,13 +72,13 @@ 例如 [1,2,2,2,2,1]这样的数组,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png) +![](https://file.kamacoder.com/pics/20230106170449.png) 它的摇摆序列长度是多少呢? **其实是长度是 3**,也就是我们在删除的时候 要不删除左面的三个 2,要不就删除右边的三个 2。 如图,可以统一规则,删除左边的三个 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106172613.png) +![](https://file.kamacoder.com/pics/20230106172613.png) 在图中,当 i 指向第一个 2 的时候,`prediff > 0 && curdiff = 0` ,当 i 指向最后一个 2 的时候 `prediff = 0 && curdiff < 0`。 @@ -108,7 +106,7 @@ 那么为了规则统一,针对序列[2,5],可以假设为[2,2,5],这样它就有坡度了即 preDiff = 0,如图: -![376.摆动序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174357612.png) +![376.摆动序列1](https://file.kamacoder.com/pics/20201124174357612.png) 针对以上情形,result 初始为 1(默认最右面有一个峰值),此时 curDiff > 0 && preDiff <= 0,那么 result++(计算了左面的峰值),最后得到的 result 就是 2(峰值个数为 2 即摆动序列长度为 2) @@ -147,7 +145,7 @@ public: 在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108171505.png) +![](https://file.kamacoder.com/pics/20230108171505.png) 图中,我们可以看出,版本一的代码在三个地方记录峰值,但其实结果因为是 2,因为 单调中的平坡 不能算峰值(即摆动)。 @@ -186,7 +184,7 @@ public: **本题异常情况的本质,就是要考虑平坡**, 平坡分两种,一个是 上下中间有平坡,一个是单调有平坡,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108174452.png) +![](https://file.kamacoder.com/pics/20230108174452.png) ### 思路 2(动态规划) @@ -714,7 +712,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index ba8546c6..20a94331 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 377. 组合总和 Ⅳ @@ -105,7 +103,7 @@ dp[i](考虑nums[j])可以由 dp[i - nums[j]](不考虑nums[j]) 推导 我们再来用示例中的例子推导一下: -![377.组合总和Ⅳ](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000625.png) +![377.组合总和Ⅳ](https://file.kamacoder.com/pics/20230310000625.png) 如果代码运行处的结果不是想要的结果,就把dp[i]都打出来,看看和我们推导的一不一样。 @@ -357,7 +355,3 @@ public class Solution ``` -

- - - diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 1d739173..8a2f52ae 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -466,8 +464,4 @@ bool canConstruct(char* ransomNote, char* magazine) { ``` -

- - - diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index 92246e4f..d59b7bc1 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 392.判断子序列 @@ -82,7 +80,7 @@ if (s[i - 1] != t[j - 1]),此时相当于t要删除元素,t如果把当前 因为这样的定义在dp二维矩阵中可以留出初始化的区间,如图: -![392.判断子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303173115966.png) +![392.判断子序列](https://file.kamacoder.com/pics/20210303173115966.png) 如果要是定义的dp[i][j]是以下标i为结尾的字符串s和以下标j为结尾的字符串t,初始化就比较麻烦了。 @@ -100,14 +98,14 @@ vector> dp(s.size() + 1, vector(t.size() + 1, 0)); 如图所示: -![392.判断子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303172354155.jpg) +![392.判断子序列1](https://file.kamacoder.com/pics/20210303172354155.jpg) 5. 举例推导dp数组 以示例一为例,输入:s = "abc", t = "ahbgdc",dp状态转移图如下: -![392.判断子序列2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021030317364166.jpg) +![392.判断子序列2](https://file.kamacoder.com/pics/2021030317364166.jpg) dp[i][j]表示以下标i-1为结尾的字符串s和以下标j-1为结尾的字符串t 相同子序列的长度,所以如果dp[s.size()][t.size()] 与 字符串s的长度相同说明:s与t的最长相同子序列就是s,那么s 就是 t 的子序列。 @@ -405,7 +403,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 66aff68f..69723815 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 404.左叶子之和 @@ -14,7 +12,7 @@ 示例: -![404.左叶子之和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151927654.png) +![404.左叶子之和1](https://file.kamacoder.com/pics/20210204151927654.png) ## 算法公开课 @@ -28,12 +26,12 @@ 大家思考一下如下图中二叉树,左叶子之和究竟是多少? -![404.左叶子之和](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151949672.png) +![404.左叶子之和](https://file.kamacoder.com/pics/20210204151949672.png) **其实是0,因为这棵树根本没有左叶子!** 但看这个图的左叶子之和是多少? -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902165805.png) +![图二](https://file.kamacoder.com/pics/20220902165805.png) 相信通过这两个图,大家对最左叶子的定义有明确理解了。 @@ -685,7 +683,3 @@ public int SumOfLeftLeaves(TreeNode root) ``` -

- - - diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index d6fc415b..0d060ee8 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 406.根据身高重建队列 @@ -63,7 +61,7 @@ 以图中{5,2} 为例: -![406.根据身高重建队列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216201851982.png) +![406.根据身高重建队列](https://file.kamacoder.com/pics/20201216201851982.png) 按照身高排序之后,优先按身高高的people的k来插入,后序插入节点也不会影响前面已经插入的节点,最终按照k的规则完成了队列。 @@ -422,7 +420,3 @@ public class Solution -

- - - diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 55ed7ad2..79b4d4f7 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 416. 分割等和子集 @@ -60,7 +58,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -### 01背包问题 +## 01背包问题 01背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 @@ -92,7 +90,7 @@ 动规五部曲分析如下: -1. 确定dp数组以及下标的含义 +### 1. 确定dp数组以及下标的含义 01背包中,dp[j] 表示: 容量(所能装的重量)为j的背包,所背的物品价值最大可以为dp[j]。 @@ -104,7 +102,7 @@ 而dp[6] 就可以等于6了,放进1 和 5,那么dp[6] == 6,说明背包装满了。 -2. 确定递推公式 +### 2. 确定递推公式 01背包的递推公式为:dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); @@ -113,7 +111,7 @@ 所以递推公式:dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]); -3. dp数组如何初始化 +### 3. dp数组如何初始化 在01背包,一维dp如何初始化,已经讲过, @@ -133,7 +131,7 @@ vector dp(10001, 0); ``` -4. 确定遍历顺序 +### 4. 确定遍历顺序 在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历! @@ -148,7 +146,7 @@ for(int i = 0; i < nums.size(); i++) { } ``` -5. 举例推导dp数组 +### 5. 举例推导dp数组 dp[j]的数值一定是小于等于j的。 @@ -157,7 +155,7 @@ dp[j]的数值一定是小于等于j的。 用例1,输入[1,5,11,5] 为例,如图: -![416.分割等和子集2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110104240545.png) +![416.分割等和子集2](https://file.kamacoder.com/pics/20210110104240545.png) 最后dp[11] == 11,说明可以将这个数组分割成两个子集,使得两个子集的元素和相等。 @@ -801,7 +799,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index 5156ce22..116cd08e 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -20,7 +18,7 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230129103212.png) +![](https://file.kamacoder.com/pics/20230129103212.png) * 输入: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] * 输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] @@ -132,11 +130,11 @@ public: 从太平洋边上节点出发,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220722103029.png) +![图一](https://file.kamacoder.com/pics/20220722103029.png) 从大西洋边上节点出发,如图: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220722103330.png) +![图二](https://file.kamacoder.com/pics/20220722103330.png) 按照这样的逻辑,就可以写出如下遍历代码:(详细注释) @@ -837,7 +835,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index d6321315..04845ea7 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 435. 无重叠区间 @@ -46,7 +44,7 @@ 这里记录非交叉区间的个数还是有技巧的,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230201164134.png) +![](https://file.kamacoder.com/pics/20230201164134.png) 区间,1,2,3,4,5,6都按照右边界排好序。 @@ -495,7 +493,3 @@ public class Solution -

- - - diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 831655e8..406116a3 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 二叉搜索树删除节点就涉及到结构调整了 @@ -22,7 +20,7 @@ 示例: -![450.删除二叉搜索树中的节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20201020171048265.png) +![450.删除二叉搜索树中的节点](https://file.kamacoder.com/pics/20201020171048265.png) ## 算法公开课 @@ -836,7 +834,3 @@ def delete_node(root, key) end ``` -

- - - diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index 14456f92..17d21cd1 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 452. 用最少数量的箭引爆气球 @@ -78,7 +76,7 @@ 以题目示例: [[10,16],[2,8],[1,6],[7,12]]为例,如图:(方便起见,已经排序) -![452.用最少数量的箭引爆气球](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123101929791.png) +![452.用最少数量的箭引爆气球](https://file.kamacoder.com/pics/20201123101929791.png) 可以看出首先第一组重叠气球,一定是需要一个箭,气球3,的左边界大于了 第一组重叠气球的最小右边界,所以再需要一支箭来射气球3了。 @@ -357,7 +355,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index af19f5f7..a26071a1 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 需要哈希的地方都能找到map的身影 @@ -526,7 +524,3 @@ def two_sum_mapping(nums1, nums2) end ``` -

- - - diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 5e8fb730..2a6ade1b 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 455.分发饼干 @@ -48,7 +46,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230405225628.png) +![](https://file.kamacoder.com/pics/20230405225628.png) 这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。 @@ -91,7 +89,7 @@ public: 如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 : -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230112102848.png) +![](https://file.kamacoder.com/pics/20230112102848.png) if 里的 index 指向 胃口 10, for 里的 i 指向饼干 9,因为 饼干 9 满足不了 胃口 10,所以 i 持续向前移动,而 index 走不到` s[index] >= g[i]` 的逻辑,所以 index 不会移动,那么当 i 持续向前移动,最后所有的饼干都匹配不上。 @@ -434,7 +432,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index bdced0ef..627a27a4 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > KMP算法还能干这个 @@ -48,13 +46,13 @@ 当一个字符串s:abcabc,内部由重复的子串组成,那么这个字符串的结构一定是这样的: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104518.png) +![图一](https://file.kamacoder.com/pics/20220728104518.png) 也就是由前后相同的子串组成。 那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前面的子串做后串,就一定还能组成一个s,如图: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104931.png) +![图二](https://file.kamacoder.com/pics/20220728104931.png) 当然,我们在判断 s + s 拼接的字符串里是否出现一个s的的时候,**要刨除 s + s 的首字符和尾字符**,这样避免在s+s中搜索出原来的s,我们要搜索的是中间拼接出来的s。 @@ -66,11 +64,11 @@ 如图,字符串s,图中数字为数组下标,在 s + s 拼接后, 不算首尾字符,中间凑成s字符串。 (图中数字为数组下标) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115555.png) +![](https://file.kamacoder.com/pics/20240910115555.png) 图中,因为中间拼接成了s,根据红色框 可以知道 s[4] = s[0], s[5] = s[1], s[0] = s[2], s[1] = s[3] s[2] = s[4] ,s[3] = s[5] -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115819.png) +![](https://file.kamacoder.com/pics/20240910115819.png) 以上相等关系我们串联一下: @@ -85,7 +83,7 @@ s[5] = s[1] = s[3] 这里可以有录友想,凭什么就是这样组成的s呢,我换一个方式组成s 行不行,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910120751.png) +![](https://file.kamacoder.com/pics/20240910120751.png) s[3] = s[0],s[4] = s[1] ,s[5] = s[2],s[0] = s[3],s[1] = s[4],s[2] = s[5] @@ -103,7 +101,7 @@ s[0] s[1] s[2] = s[3] s[4] s[5] 如果是这样的呢,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910121236.png) +![](https://file.kamacoder.com/pics/20240910121236.png) s[1] = s[0],s[2] = s[1] ,s[3] = s[2],s[4] = s[3],s[5] = s[4],s[0] = s[5] @@ -167,23 +165,23 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 那么相同前后缀可以是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110257.png) +![](https://file.kamacoder.com/pics/20240913110257.png) 也可以是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110316.png) +![](https://file.kamacoder.com/pics/20240913110316.png) 最长的相等前后缀,也就是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110841.png) +![](https://file.kamacoder.com/pics/20240913110841.png) 这里有录友就想:如果字符串s 是由最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114348.png) +![](https://file.kamacoder.com/pics/20240913114348.png) 如果这样的话,因为前后缀要相同,所以 p2 = p1,p3 = p2,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114818.png) +![](https://file.kamacoder.com/pics/20240913114818.png) p2 = p1,p3 = p2 即: p1 = p2 = p3 @@ -205,7 +203,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 情况一, 最长相等前后缀不包含的子串的长度 比 字符串s的一半的长度还大,那一定不是字符串s的重复子串,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240911110236.png) +![](https://file.kamacoder.com/pics/20240911110236.png) 图中:前后缀不包含的子串的长度 大于 字符串s的长度的 二分之一 @@ -213,7 +211,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 情况二,最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910174249.png) +![](https://file.kamacoder.com/pics/20240910174249.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,所以 s[0] 一定和 s[2]相同,s[1] 一定和 s[3]相同,即:,s[0]s[1]与s[2]s[3]相同 。 @@ -236,7 +234,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 那么它的最长相同前后缀,就不是上图中的前后缀,而是这样的的前后缀: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910175053.png) +![](https://file.kamacoder.com/pics/20240910175053.png) 录友可能再问,由一个字符组成的字符串,最长相等前后缀凭什么就是这样的。 @@ -252,7 +250,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 **情况三,最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除得情况**,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913115854.png) +![](https://file.kamacoder.com/pics/20240913115854.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,t[2] 与 k[2]相同。 @@ -689,6 +687,29 @@ var repeatedSubstringPattern = function (s) { }; ``` +> 正则匹配 +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var repeatedSubstringPattern = function(s) { + let reg = /^(\w+)\1+$/ + return reg.test(s) +}; +``` +> 移动匹配 +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var repeatedSubstringPattern = function (s) { + let ss = s + s; + return ss.substring(1, ss.length - 1).includes(s); +}; +``` + ### TypeScript: > 前缀表统一减一 @@ -894,8 +915,10 @@ impl Solution { } ``` ### C# + +> 前缀表不减一 + ```csharp -// 前缀表不减一 public bool RepeatedSubstringPattern(string s) { if (s.Length == 0) @@ -920,6 +943,13 @@ public int[] GetNext(string s) } ``` +> 移动匹配 +```csharp +public bool RepeatedSubstringPattern(string s) { + string ss = (s + s).Substring(1, (s + s).Length - 2); + return ss.Contains(s); +} +``` ### C ```c @@ -967,7 +997,3 @@ bool repeatedSubstringPattern(char* s) { ``` -

- - - diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md index 5261d6c2..40ddc57d 100644 --- a/problems/0463.岛屿的周长.md +++ b/problems/0463.岛屿的周长.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -17,7 +15,7 @@ 岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230829180848.png) +![](https://file.kamacoder.com/pics/20230829180848.png) * 输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] * 输出:16 @@ -433,7 +431,3 @@ function islandPerimeter(grid: number[][]): number { ``` -

- - - diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index e514e729..8166b39a 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 474.一和零 @@ -53,7 +51,7 @@ 其实本题并不是多重背包,再来看一下这个图,捋清几种背包的关系 -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407-20230310132423205.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407-20230310132423205.png) 多重背包是每个物品,数量不同的情况。 @@ -129,7 +127,7 @@ for (string str : strs) { // 遍历物品 最后dp数组的状态如下所示: -![474.一和零](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120111201512.jpg) +![474.一和零](https://file.kamacoder.com/pics/20210120111201512.jpg) 以上动规五部曲分析完毕,C++代码如下: @@ -261,8 +259,70 @@ public: ## 其他语言版本 - ### Java + +三维DP数组实现 + +```java +class Solution { + public int findMaxForm(String[] strs, int m, int n) { + /// 数组有三个维度 + // 第一个维度:取前面的几个字符串 + // 第二个维度:0的数量限制(背包维度 1 容量) + // 第三个维度:1的数量限制(背包维度 2 容量) + int[][][] dpArr = new int[strs.length][m + 1][n + 1]; + + /// 初始化dpArr数组 + // 计算第一个字符串的零数量和1数量 + int zeroNum = 0; + int oneNum = 0; + for (char c : strs[0].toCharArray()) { + if (c == '0') { + zeroNum++; + } else { + oneNum++; + } + } + // 当0数量、1数量都容得下第一个字符串时,将DP数组的相应位置初始化为1,因为当前的子集数量为1 + for (int j = zeroNum; j <= m; j++) { + for (int k = oneNum; k <= n; k++) { + dpArr[0][j][k] = 1; + } + } + /// 依次填充加入第i个字符串之后的DP数组 + for (int i = 1; i < strs.length; i++) { + zeroNum = 0; + oneNum = 0; + for (char c : strs[i].toCharArray()) { + if (c == '0') { + zeroNum++; + } else { + oneNum++; + } + } + for (int j = 0; j <= m; j++) { + for (int k = 0; k <= n; k++) { + if (j >= zeroNum && k >= oneNum) { + // --if-- 当0数量维度和1数量维度的容量都大于等于当前字符串的0数量和1数量时,才考虑是否将当前字符串放入背包 + // 不放入第i个字符串,子集数量仍为 dpArr[i - 1][j][k] + // 放入第i个字符串,需要在0维度腾出 zeroNum 个容量,1维度腾出 oneNum 个容量,然后放入当前字符串,即 dpArr[i - 1][j - zeroNum][k - oneNum] + 1) + dpArr[i][j][k] = Math.max(dpArr[i - 1][j][k], dpArr[i - 1][j - zeroNum][k - oneNum] + 1); + } else { + // --if-- 无法放入第i个字符串,子集数量仍为 dpArr[i - 1][j][k] + dpArr[i][j][k] = dpArr[i - 1][j][k]; + } + } + } + } + return dpArr[dpArr.length - 1][m][n]; + } +} +``` + + + +二维DP数组实现 + ```Java class Solution { public int findMaxForm(String[] strs, int m, int n) { @@ -678,7 +738,4 @@ public class Solution ``` -

- - - + diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 7832095a..b3171c8a 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 和子集问题有点像,但又处处是陷阱 @@ -47,7 +45,7 @@ 为了有鲜明的对比,我用[4, 7, 6, 7]这个数组来举例,抽象为树形结构如图: -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124200229824.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201124200229824.png) @@ -81,7 +79,7 @@ if (path.size() > 1) { * 单层搜索逻辑 -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124200229824-20230310131640070.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201124200229824-20230310131640070.png) 在图中可以看出,**同一父节点下的同层上使用过的元素就不能再使用了** 那么单层搜索代码如下: @@ -640,7 +638,3 @@ public class Solution { } ``` -

- - - diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 4a1fc6ab..a23e1743 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -165,7 +163,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 先只考虑物品0,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808161747.png) +![](https://file.kamacoder.com/pics/20240808161747.png) (这里的所有物品,都是题目中的数字1)。 @@ -179,7 +177,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 接下来 考虑 物品0 和 物品1,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162052.png) +![](https://file.kamacoder.com/pics/20240808162052.png) 装满背包容量为0 的方法个数是1,即 放0件物品。 @@ -193,7 +191,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 接下来 考虑 物品0 、物品1 和 物品2 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162533.png) +![](https://file.kamacoder.com/pics/20240808162533.png) 装满背包容量为0 的方法个数是1,即 放0件物品。 @@ -209,17 +207,17 @@ if (abs(target) > sum) return 0; // 此时没有方案 如图红色部分: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808163312.png) +![](https://file.kamacoder.com/pics/20240808163312.png) dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物品1 和 物品2, 如图所示,三种方法: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826111946.png) +![](https://file.kamacoder.com/pics/20240826111946.png) **容量为2 的背包,如果不放 物品2 有几种方法呢**? 有 dp[1][2] 种方法,即 背包容量为2,只考虑物品0 和 物品1 ,有 dp[1][2] 种方法,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826112805.png) +![](https://file.kamacoder.com/pics/20240826112805.png) **容量为2 的背包, 如果放 物品2 有几种方法呢**? @@ -231,7 +229,7 @@ dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826113043.png) +![](https://file.kamacoder.com/pics/20240826113043.png) 有录友可能疑惑,这里计算的是放满 容量为2的背包 有几种方法,那物品2去哪了? @@ -241,7 +239,7 @@ dp[2][2] = 容量为2的背包不放物品2有几种方法 + 容量为2的背包 所以 dp[2][2] = dp[1][2] + dp[1][1] ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826113258.png) +![](https://file.kamacoder.com/pics/20240826113258.png) 以上过程,抽象化如下: @@ -268,11 +266,11 @@ else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; 先明确递推的方向,如图,求解 dp[2][2] 是由 上方和左上方推出。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826115800.png) +![](https://file.kamacoder.com/pics/20240826115800.png) 那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827103507.png) +![](https://file.kamacoder.com/pics/20240827103507.png) 关于dp[0][0]的值,在上面的递推公式讲解中已经讲过,装满背包容量为0 的方法数量是1,即 放0件物品。 @@ -325,7 +323,7 @@ for (int i = 0; i < nums.size(); i++) { 例如下图,如果上方没数值,左上方没数值,就无法推出 dp[2][2]。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827105427.png) +![](https://file.kamacoder.com/pics/20240827105427.png) 那么是先 从上到下 ,再从左到右遍历,例如这样: @@ -351,11 +349,11 @@ for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 这里我再画图讲一下,以求dp[2][2]为例,当先从上到下,再从左到右遍历,矩阵是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827110933.png) +![](https://file.kamacoder.com/pics/20240827110933.png) 当先从左到右,再从上到下遍历,矩阵是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827111013.png) +![](https://file.kamacoder.com/pics/20240827111013.png) 这里大家可以看出,无论是以上哪种遍历,都不影响 dp[2][2]的求值,用来 推导 dp[2][2] 的数值都在。 @@ -368,7 +366,7 @@ bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827111612.png) +![](https://file.kamacoder.com/pics/20240827111612.png) 这么大的矩阵,我们是可以自己手动模拟出来的。 @@ -447,7 +445,7 @@ bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274.jpg) +![](https://file.kamacoder.com/pics/20210125120743274.jpg) 大家可以和 二维dp数组的打印结果做一下对比。 @@ -825,30 +823,69 @@ func abs(x int) int { ### JavaScript ```javascript +/** + * 题目来源: {@link https://leetcode.cn/problems/target-sum/} + * + * 题解来源: {@link https://programmercarl.com/0494.%E7%9B%AE%E6%A0%87%E5%92%8C.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE} + * + * 时间复杂度: O(n * C), C 为数组元素总和与目标值之和的一半 + * + * 空间复杂度: O(C) + * + * @param { number[] } nums + * @param { number } target + * @return { number } + */ const findTargetSumWays = (nums, target) => { + // 原题目可转化为: + // + // 将所有元素划分为 2 个集合, + // 一个集合中包含所有要添加 "+" 号的元素, 一个集合中包含所有要添加 "-" 号的元素 + // + // 设两个集合的元素和分别为 positive 和 negative, 所有元素总和为 sum, 那么有如下等式: + // positive + negative = sum (1) + // positive - negative = target (2) + // (1) 与 (2) 联立可得: positive = (sum + target) / 2, + // 所以如果能从原数组中取出若干个元素形成 1 个元素总和为 (sum + target) / 2 的集合, + // 就算得到了 1 种满足题意的组合方法 + // + // 因此, 所求变为: 有多少种取法, 可使得容量为 (sum + target) / 2 的背包被装满? - const sum = nums.reduce((a, b) => a+b); + const sum = nums.reduce((a, b) => a + b); - if(Math.abs(target) > sum) { + if (Math.abs(target) > sum) { return 0; } - if((target + sum) % 2) { + if ((target + sum) % 2) { return 0; } - const halfSum = (target + sum) / 2; - - let dp = new Array(halfSum+1).fill(0); + const bagWeight = (target + sum) / 2; + + // 1. dp 数组的含义 + // dp[j]: 装满容量为 j 的背包, 有 dp[j] 种方法 + let dp = new Array(bagWeight + 1).fill(0); + + // 2. 递推公式 + // dp[j] = Σ(dp[j - nums[j]]), (j ∈ [0, j] 且 j >= nums[j]) + // 因为 dp[j - nums[j]] 表示: 装满容量为 j - nums[j] 背包有 dp[j - nums[j]] 种方法 + // 而容量为 j - nums[j] 的背包只需要再将 nums[j] 放入背包就能使得背包容量达到 j + // 因此, 让背包容量达到 j 有 Σ(dp[j - nums[j]]) 种方法 + + // 3. dp 数组如何初始化 + // dp[0] = 1, dp[1 ~ bagWeight] = 0 dp[0] = 1; - - for(let i = 0; i < nums.length; i++) { - for(let j = halfSum; j >= nums[i]; j--) { + + // 4. 遍历顺序 + // 先物品后背包, 物品从前往后遍历, 背包容量从后往前遍历 + for (let i = 0; i < nums.length; i++) { + for (let j = bagWeight; j >= nums[i]; j--) { dp[j] += dp[j - nums[i]]; } } - return dp[halfSum]; + return dp[bagWeight]; }; ``` @@ -985,8 +1022,4 @@ public class Solution ``` -

- - - diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 02e73a58..628149b7 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 496.下一个更大元素 I @@ -507,7 +505,3 @@ impl Solution { -

- - - diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index c89f8031..8cca8e65 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 二叉树上应该怎么求,二叉搜索树上又应该怎么求? @@ -25,7 +23,7 @@ 给定 BST [1,null,2,2], -![501. 二叉搜索树中的众数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014221532206.png) +![501. 二叉搜索树中的众数](https://file.kamacoder.com/pics/20201014221532206.png) 返回[2]. @@ -146,7 +144,7 @@ public: 如图: -![501.二叉搜索树中的众数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204152758889.png) +![501.二叉搜索树中的众数1](https://file.kamacoder.com/pics/20210204152758889.png) 中序遍历代码如下: @@ -1052,7 +1050,3 @@ public class Solution -

- - - diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index b466337d..93924483 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 503.下一个更大元素II @@ -358,7 +356,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 1c4127fc..b2e56a61 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 509. 斐波那契数 @@ -151,7 +149,7 @@ public: * 时间复杂度:O(2^n) * 空间复杂度:O(n),算上了编程语言中实现递归的系统栈所占空间 -这个递归的时间复杂度大家画一下树形图就知道了,如果不清晰的同学,可以看这篇:[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html) +这个递归的时间复杂度大家画一下树形图就知道了,如果不清晰的同学,可以看这篇:[通过一道面试题目,讲一讲递归算法的时间复杂度!](./前序/递归算法的时间复杂度.md) ## 总结 @@ -476,7 +474,3 @@ public class Solution -

- - - diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index c7446726..4098cb7b 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 513.找树左下角的值 @@ -14,11 +12,11 @@ 示例 1: -![513.找树左下角的值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204152956836.png) +![513.找树左下角的值](https://file.kamacoder.com/pics/20210204152956836.png) 示例 2: -![513.找树左下角的值1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153017586.png) +![513.找树左下角的值1](https://file.kamacoder.com/pics/20210204153017586.png) ## 算法公开课 @@ -764,7 +762,3 @@ public class Solution // @lc code=end ``` -

- - - diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index 166310aa..5e456ac9 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -58,7 +56,7 @@ 如果s[i]与s[j]相同,那么dp[i][j] = dp[i + 1][j - 1] + 2; 如图: -![516.最长回文子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151350563.jpg) +![516.最长回文子序列](https://file.kamacoder.com/pics/20210127151350563.jpg) (如果这里看不懂,回忆一下dp[i][j]的定义) @@ -70,7 +68,7 @@ 那么dp[i][j]一定是取最大的,即:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); -![516.最长回文子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151420476.jpg) +![516.最长回文子序列1](https://file.kamacoder.com/pics/20210127151420476.jpg) 代码如下: @@ -99,7 +97,7 @@ for (int i = 0; i < s.size(); i++) dp[i][i] = 1; 从递归公式中,可以看出,dp[i][j] 依赖于 dp[i + 1][j - 1] ,dp[i + 1][j] 和 dp[i][j - 1],如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230102172155.png) +![](https://file.kamacoder.com/pics/20230102172155.png) **所以遍历i的时候一定要从下到上遍历,这样才能保证下一行的数据是经过计算的**。 @@ -123,7 +121,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 输入s:"cbbd" 为例,dp数组状态如图: -![516.最长回文子序列3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151521432.jpg) +![516.最长回文子序列3](https://file.kamacoder.com/pics/20210127151521432.jpg) 红色框即:dp[0][s.size() - 1]; 为最终结果。 @@ -299,7 +297,3 @@ impl Solution { -

- - - diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 835df852..95122a7c 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 518.零钱兑换II @@ -138,7 +136,7 @@ 那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827103507.png) +![](https://file.kamacoder.com/pics/20240827103507.png) 这里首先要关注的就是 dp[0][0] 应该是多少? @@ -298,7 +296,7 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 输入: amount = 5, coins = [1, 2, 5] ,dp状态图如下: -![518.零钱兑换II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120181331461.jpg) +![518.零钱兑换II](https://file.kamacoder.com/pics/20210120181331461.jpg) 最后红色框dp[amount]为最终结果。 @@ -589,7 +587,3 @@ public class Solution ``` -

- - - diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 2533a618..466bd744 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 利用二叉搜索树的特性搞起! @@ -15,7 +13,7 @@ 示例: -![530二叉搜索树的最小绝对差](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014223400123.png) +![530二叉搜索树的最小绝对差](https://file.kamacoder.com/pics/20201014223400123.png) 提示:树中至少有 2 个节点。 @@ -72,7 +70,7 @@ public: 如图: -![530.二叉搜索树的最小绝对差](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153247458.png) +![530.二叉搜索树的最小绝对差](https://file.kamacoder.com/pics/20210204153247458.png) 一些同学不知道在递归中如何记录前一个节点的指针,其实实现起来是很简单的,大家只要看过一次,写过一次,就掌握了。 @@ -153,23 +151,27 @@ public: 递归 ```java class Solution { - TreeNode pre;// 记录上一个遍历的结点 + TreeNode pre; // 记录上一个遍历的结点 int result = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { - if(root==null)return 0; - traversal(root); - return result; + if (root == null) + return 0; + traversal(root); + return result; } - public void traversal(TreeNode root){ - if(root==null)return; - //左 + + public void traversal(TreeNode root) { + if (root == null) + return; + // 左 traversal(root.left); - //中 - if(pre!=null){ - result = Math.min(result,root.val-pre.val); + // 中 + if (pre != null) { + result = Math.min(result, root.val - pre.val); } pre = root; - //右 + // 右 traversal(root.right); } } @@ -182,22 +184,27 @@ class Solution { TreeNode pre = null; int result = Integer.MAX_VALUE; - if(root != null) + if (root != null) stack.add(root); - while(!stack.isEmpty()){ + + // 中序遍历(左中右),由于栈先入后出,反序(右中左) + while (!stack.isEmpty()) { TreeNode curr = stack.peek(); - if(curr != null){ + if (curr != null) { stack.pop(); - if(curr.right != null) + // 右 + if (curr.right != null) stack.add(curr.right); + // 中(先用null标记) stack.add(curr); stack.add(null); - if(curr.left != null) + // 左 + if (curr.left != null) stack.add(curr.left); - }else{ + } else { // 中(遇到null再处理) stack.pop(); TreeNode temp = stack.pop(); - if(pre != null) + if (pre != null) result = Math.min(result, temp.val - pre.val); pre = temp; } @@ -670,7 +677,4 @@ public class Solution ``` -

- - - + diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index b95b5854..45bf1f96 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 538.把二叉搜索树转换为累加树 @@ -20,7 +18,7 @@ 示例 1: -![538.把二叉搜索树转换为累加树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201023160751832.png) +![538.把二叉搜索树转换为累加树](https://file.kamacoder.com/pics/20201023160751832.png) * 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] * 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] @@ -69,7 +67,7 @@ 遍历顺序如图所示: -![538.把二叉搜索树转换为累加树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153440666.png) +![538.把二叉搜索树转换为累加树](https://file.kamacoder.com/pics/20210204153440666.png) 本题依然需要一个pre指针记录当前遍历节点cur的前一个节点,这样才方便做累加。 @@ -549,7 +547,3 @@ public class Solution -

- - - diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index b3e7b022..2bbe6cff 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -516,8 +514,4 @@ impl Solution { } ``` -

- - - diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index b9f9ad96..7f7d30f6 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 583. 两个字符串的删除操作 @@ -83,7 +81,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 以word1:"sea",word2:"eat"为例,推导dp数组状态图如下: -![583.两个字符串的删除操作1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210714101750205.png) +![583.两个字符串的删除操作1](https://file.kamacoder.com/pics/20210714101750205.png) 以上分析完毕,代码如下: @@ -470,7 +468,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 530350ac..755200fe 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 617.合并二叉树 @@ -15,7 +13,7 @@ 示例 1: -![617.合并二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000854.png) +![617.合并二叉树](https://file.kamacoder.com/pics/20230310000854.png) 注意: 合并必须从两个树的根节点开始。 @@ -804,7 +802,3 @@ public TreeNode MergeTrees(TreeNode root1, TreeNode root2) ``` -

- - - diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index cf32d7ed..72829535 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 647. 回文子串 @@ -50,7 +48,7 @@ dp[i] 和 dp[i-1] ,dp[i + 1] 看上去都没啥关系。 所以我们要看回文串的性质。 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230102170752.png) +![](https://file.kamacoder.com/pics/20230102170752.png) 我们在判断字符串S是否是回文,那么如果我们知道 s[1],s[2],s[3] 这个子串是回文的,那么只需要比较 s[0]和s[4]这两个元素是否相同,如果相同的话,这个字符串s 就是回文串。 @@ -108,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473-20230310132134822.jpg) +![647.回文子串](https://file.kamacoder.com/pics/20210121171032473-20230310132134822.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -138,7 +136,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951-20230310132153163.jpg) +![647.回文子串1](https://file.kamacoder.com/pics/20210121171059951-20230310132153163.jpg) 图中有6个true,所以就是有6个回文子串。 @@ -613,7 +611,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0649.Dota2参议院.md b/problems/0649.Dota2参议院.md index 1540a601..e77070fc 100644 --- a/problems/0649.Dota2参议院.md +++ b/problems/0649.Dota2参议院.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -284,7 +282,3 @@ function predictPartyVictory(senate: string): string { -

- - - diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index fed9b2b9..b8841a8b 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 654.最大二叉树 @@ -19,7 +17,7 @@ 示例 : -![654.最大二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204154534796.png) +![654.最大二叉树](https://file.kamacoder.com/pics/20210204154534796.png) 提示: @@ -599,7 +597,3 @@ public TreeNode ConstructMaximumBinaryTree(int[] nums) ``` -

- - - diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index eccfef3a..89993b6f 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 657. 机器人能否返回原点 @@ -182,7 +180,3 @@ var judgeCircle = function (moves) { -

- - - diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 32573386..f4ded2c4 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -16,9 +14,9 @@ 给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。 -![669.修剪二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173115788.png) +![669.修剪二叉搜索树](https://file.kamacoder.com/pics/20201014173115788.png) -![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173219142.png) +![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20201014173219142.png) ## 算法公开课 @@ -52,7 +50,7 @@ public: 我们在重新关注一下第二个示例,如图: -![669.修剪二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155302751.png) +![669.修剪二叉搜索树](https://file.kamacoder.com/pics/20210204155302751.png) **所以以上的代码是不可行的!** @@ -62,7 +60,7 @@ public: 在上图中我们发现节点0并不符合区间要求,那么将节点0的右孩子 节点2 直接赋给 节点3的左孩子就可以了(就是把节点0从二叉树中移除),如图: -![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155327203.png) +![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20210204155327203.png) 理解了最关键部分了我们再递归三部曲: @@ -129,7 +127,7 @@ return root; 在回顾一下上面的代码,针对下图中二叉树的情况: -![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155327203-20230310120126738.png) +![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20210204155327203-20230310120126738.png) 如下代码相当于把节点0的右孩子(节点2)返回给上一层, @@ -587,7 +585,3 @@ public TreeNode TrimBST(TreeNode root, int low, int high) -

- - - diff --git a/problems/0673.最长递增子序列的个数.md b/problems/0673.最长递增子序列的个数.md index 0366ee80..92009f5b 100644 --- a/problems/0673.最长递增子序列的个数.md +++ b/problems/0673.最长递增子序列的个数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 673.最长递增子序列的个数 @@ -180,7 +178,7 @@ for (int i = 0; i < nums.size(); i++) { 输入:[1,3,5,4,7] -![673.最长递增子序列的个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000656.png) +![673.最长递增子序列的个数](https://file.kamacoder.com/pics/20230310000656.png) **如果代码写出来了,怎么改都通过不了,那么把dp和count打印出来看看对不对!** @@ -361,7 +359,3 @@ var findNumberOfLIS = function(nums) { -

- - - diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 8b967092..16bb2f18 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 674. 最长连续递增序列 @@ -87,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) { 已输入nums = [1,3,5,4,7]为例,dp数组状态如下: -![674.最长连续递增序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204103529742.jpg) +![674.最长连续递增序列](https://file.kamacoder.com/pics/20210204103529742.jpg) **注意这里要取dp[i]里的最大值,所以dp[2]才是结果!** @@ -514,7 +512,3 @@ func findLengthOfLCIS(nums: Array): Int64 { -

- - - diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md index 78085490..8a7234df 100644 --- a/problems/0684.冗余连接.md +++ b/problems/0684.冗余连接.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 684.冗余连接 @@ -14,7 +12,7 @@ 请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727150215.png) +![](https://file.kamacoder.com/pics/20210727150215.png) 提示: * n == edges.length @@ -87,7 +85,7 @@ void join(int u, int v) { 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104720.png) +![](https://file.kamacoder.com/pics/20230604104720.png) 节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 @@ -97,7 +95,7 @@ void join(int u, int v) { 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104330.png) +![](https://file.kamacoder.com/pics/20230604104330.png) 已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 @@ -379,7 +377,3 @@ var findRedundantConnection = function(edges) { -

- - - diff --git a/problems/0685.冗余连接II.md b/problems/0685.冗余连接II.md index 3f489d82..66f7bfe1 100644 --- a/problems/0685.冗余连接II.md +++ b/problems/0685.冗余连接II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 685.冗余连接II @@ -18,9 +16,9 @@ 返回一条能删除的边,使得剩下的图是有 n 个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727151057.png) +![](https://file.kamacoder.com/pics/20210727151057.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727151118.png) +![](https://file.kamacoder.com/pics/20210727151118.png) 提示: @@ -620,7 +618,3 @@ var findRedundantDirectedConnection = function(edges) { -

- - - diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 11b638d4..972a9995 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 695. 岛屿的最大面积 @@ -16,7 +14,7 @@ 计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220729111528.png) +![](https://file.kamacoder.com/pics/20220729111528.png) * 输入:grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] * 输出:6 @@ -29,7 +27,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 @@ -44,7 +42,7 @@ 这里其实涉及到dfs的两种写法。 -写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 +写法一,dfs处理当前节点的相邻节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 ```CPP // 版本一 @@ -87,7 +85,7 @@ public: }; ``` -写法二,dfs处理当前节点,即即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地 +写法二,dfs处理当前节点,即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地 dfs ```CPP @@ -709,7 +707,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 9ec51524..0c373f61 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 700.二叉搜索树中的搜索 @@ -14,7 +12,7 @@ 例如, -![700.二叉搜索树中的搜索](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155522476.png) +![700.二叉搜索树中的搜索](https://file.kamacoder.com/pics/20210204155522476.png) 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 @@ -126,7 +124,7 @@ public: 中间节点如果大于3就向左走,如果小于3就向右走,如图: -![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20200812190213280.png) +![二叉搜索树](https://file.kamacoder.com/pics/20200812190213280.png) 所以迭代法代码如下: @@ -508,7 +506,3 @@ public TreeNode SearchBST(TreeNode root, int val) ``` -

- - - diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 25d39486..6ce9ef33 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 701.二叉搜索树中的插入操作 @@ -14,7 +12,7 @@ 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。 -![701.二叉搜索树中的插入操作](https://code-thinking-1253855093.file.myqcloud.com/pics/20201019173259554.png) +![701.二叉搜索树中的插入操作](https://file.kamacoder.com/pics/20201019173259554.png) 提示: @@ -724,7 +722,3 @@ public TreeNode InsertIntoBST(TreeNode root, int val) { ``` -

- - - diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index d86146d6..0ce2f3b8 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 704. 二分查找 @@ -61,7 +59,7 @@ 例如在数组:1,2,3,4,7,9,10中查找元素2,如图所示: -![704.二分查找](https://code-thinking-1253855093.file.myqcloud.com/pics/20210311153055723.jpg) +![704.二分查找](https://file.kamacoder.com/pics/20210311153055723.jpg) 代码如下:(详细注释) @@ -104,7 +102,7 @@ public: 在数组:1,2,3,4,7,9,10中查找元素2,如图所示:(**注意和方法一的区别**) -![704.二分查找1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210311153123632.jpg) +![704.二分查找1](https://file.kamacoder.com/pics/20210311153123632.jpg) 代码如下:(详细注释) @@ -837,7 +835,3 @@ class Solution { ``` -

- - - diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index ed1726d9..a2b2803b 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 听说这道题目把链表常见的五个操作都覆盖了? @@ -22,7 +20,7 @@ * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。 -![707示例](https://code-thinking-1253855093.file.myqcloud.com/pics/20200814200558953.png) +![707示例](https://file.kamacoder.com/pics/20200814200558953.png) ## 算法公开课 @@ -37,10 +35,10 @@ 如果对链表的虚拟头结点不清楚,可以看这篇文章:[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html) 删除链表节点: -![链表-删除节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195114541.png) +![链表-删除节点](https://file.kamacoder.com/pics/20200806195114541.png) 添加链表节点: -![链表-添加节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195134331.png) +![链表-添加节点](https://file.kamacoder.com/pics/20200806195134331.png) 这道题目设计链表的五个接口: * 获取链表第index个节点的数值 @@ -1191,6 +1189,160 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { */ ``` +```js +/** + 定义双头节点的结构:同时包含前指针`prev`和后指针next` +*/ +class Node { + constructor(val, prev, next) { + this.val = val + this.prev = prev + this.next = next + } +} + +/** + 双链表:维护 `head` 和 `tail` 两个哨兵节点,这样可以简化对于中间节点的操作 + 并且维护 `size`,使得能够以O(1)时间判断操作是否合法 +*/ +var MyLinkedList = function () { + this.tail = new Node(-1) + this.head = new Node(-1) + this.tail.prev = this.head + this.head.next = this.tail + this.size = 0 +}; + +/** + * 获取在index处节点的值 + * + * @param {number} index + * @return {number} + * + * 时间复杂度: O(n) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.get = function (index) { + // 当索引超出范围时,返回-1 + if (index > this.size) { + return -1 + } + + let cur = this.head + for (let i = 0; i <= index; i++) { + cur = cur.next + } + + return cur.val +}; + +/** + * 在链表头部添加一个新节点 + * + * @param {number} val + * @return {void} + * + * 时间复杂度: O(1) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.addAtHead = function (val) { + /** + head <-> [newNode] <-> originNode + */ + this.size++ + const originNode = this.head.next + // 创建新节点,并建立连接 + const newNode = new Node(val, this.head, originNode) + + // 取消原前后结点的连接 + this.head.next = newNode + originNode.prev = newNode +}; + +/** + * 在链表尾部添加一个新节点 + * + * @param {number} val + * @return {void} + * + * 时间复杂度: O(1) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.addAtTail = function (val) { + /** + originNode <-> [newNode] <-> tail + */ + this.size++ + const originNode = this.tail.prev + + // 创建新节点,并建立连接 + const newNode = new Node(val, originNode, this.tail) + + // 取消原前后结点的连接 + this.tail.prev = newNode + originNode.next = newNode +}; + +/** + * 在指定索引位置前添加一个新节点 + * + * @param {number} index + * @param {number} val + * @return {void} + * + * 时间复杂度: O(n) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.addAtIndex = function (index, val) { + // 当索引超出范围时,直接返回 + if (index > this.size) { + return + } + this.size++ + + let cur = this.head + for (let i = 0; i < index; i++) { + cur = cur.next + } + + const new_next = cur.next + + // 创建新节点,并建立连接 + const node = new Node(val, cur, new_next) + + // 取消原前后结点的连接 + cur.next = node + new_next.prev = node +}; + +/** + * 删除指定索引位置的节点 + * + * @param {number} index + * @return {void} + * + * 时间复杂度: O(n) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.deleteAtIndex = function (index) { + // 当索引超出范围时,直接返回 + if (index >= this.size) { + return + } + + this.size-- + let cur = this.head + for (let i = 0; i < index; i++) { + cur = cur.next + } + + const new_next = cur.next.next + // 取消原前后结点的连接 + new_next.prev = cur + cur.next = new_next +}; +``` + ### TypeScript: ```TypeScript @@ -1691,8 +1843,4 @@ public class MyLinkedList } ``` -

- - - diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index e742b8c8..fb095d75 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 714. 买卖股票的最佳时机含手续费 @@ -361,7 +359,3 @@ object Solution { ``` -

- - - diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 17997b62..ebed4a0b 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 714.买卖股票的最佳时机含手续费 @@ -337,7 +335,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 1391926a..b371bd85 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 718. 最长重复子数组 @@ -97,7 +95,7 @@ for (int i = 1; i <= nums1.size(); i++) { 拿示例1中,A: [1,2,3,2,1],B: [3,2,1,4,7]为例,画一个dp数组的状态变化,如下: -![718.最长重复子数组](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011215282060.jpg) +![718.最长重复子数组](https://file.kamacoder.com/pics/2021011215282060.jpg) 以上五部曲分析完毕,C++代码如下: @@ -129,7 +127,7 @@ public: 在如下图中: -![718.最长重复子数组](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011215282060-20230310134554486.jpg) +![718.最长重复子数组](https://file.kamacoder.com/pics/2021011215282060-20230310134554486.jpg) 我们可以看出dp[i][j]都是由dp[i - 1][j - 1]推出。那么压缩为一维数组,也就是dp[j]都是由dp[j - 1]推出。 @@ -602,7 +600,3 @@ func findLength(nums1: Array, nums2: Array): Int64 { ``` -

- - - diff --git a/problems/0724.寻找数组的中心索引.md b/problems/0724.寻找数组的中心索引.md index a66a4450..bccca4f2 100644 --- a/problems/0724.寻找数组的中心索引.md +++ b/problems/0724.寻找数组的中心索引.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 724.寻找数组的中心下标 @@ -159,7 +157,3 @@ function pivotIndex(nums: number[]): number { -

- - - diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index f2cfee04..17182778 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 738.单调递增的数字 @@ -441,8 +439,4 @@ public class Solution } ``` -

- - - diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index dd633aed..ed43cf14 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -71,7 +69,7 @@ 首先先将第一个遍历元素加入单调栈 -![739.每日温度1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124434172.jpg) +![739.每日温度1](https://file.kamacoder.com/pics/20210219124434172.jpg) --------- @@ -79,65 +77,65 @@ 我们要保持一个递增单调栈(从栈头到栈底),所以将T[0]弹出,T[1]加入,此时result数组可以记录了,result[0] = 1,即T[0]右面第一个比T[0]大的元素是T[1]。 -![739.每日温度2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124504299.jpg) +![739.每日温度2](https://file.kamacoder.com/pics/20210219124504299.jpg) ----------- 加入T[2],同理,T[1]弹出 -![739.每日温度3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124527361.jpg) +![739.每日温度3](https://file.kamacoder.com/pics/20210219124527361.jpg) ------- 加入T[3],T[3] < T[2] (当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况),加T[3]加入单调栈。 -![739.每日温度4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124610761.jpg) +![739.每日温度4](https://file.kamacoder.com/pics/20210219124610761.jpg) --------- 加入T[4],T[4] == T[3] (当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况),此时依然要加入栈,不用计算距离,因为我们要求的是右面第一个大于本元素的位置,而不是大于等于! -![739.每日温度5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124633444.jpg) +![739.每日温度5](https://file.kamacoder.com/pics/20210219124633444.jpg) --------- 加入T[5],T[5] > T[4] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[4]弹出,同时计算距离,更新result -![739.每日温度6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124700567.jpg) +![739.每日温度6](https://file.kamacoder.com/pics/20210219124700567.jpg) ---------- T[4]弹出之后, T[5] > T[3] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[3]继续弹出,同时计算距离,更新result -![739.每日温度7](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124726613.jpg) +![739.每日温度7](https://file.kamacoder.com/pics/20210219124726613.jpg) ------- 直到发现T[5]小于T[st.top()],终止弹出,将T[5]加入单调栈 -![739.每日温度8](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124807715.jpg) +![739.每日温度8](https://file.kamacoder.com/pics/20210219124807715.jpg) ------- 加入T[6],同理,需要将栈里的T[5],T[2]弹出 -![739.每日温度9](https://code-thinking-1253855093.file.myqcloud.com/pics/2021021912483374.jpg) +![739.每日温度9](https://file.kamacoder.com/pics/2021021912483374.jpg) ------- 同理,继续弹出 -![739.每日温度10](https://code-thinking-1253855093.file.myqcloud.com/pics/2021021912490098.jpg) +![739.每日温度10](https://file.kamacoder.com/pics/2021021912490098.jpg) ------ 此时栈里只剩下了T[6] -![739.每日温度11](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124930156.jpg) +![739.每日温度11](https://file.kamacoder.com/pics/20210219124930156.jpg) ------------ 加入T[7], T[7] < T[6] 直接入栈,这就是最后的情况,result数组也更新完了。 -![739.每日温度12](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124957216.jpg) +![739.每日温度12](https://file.kamacoder.com/pics/20210219124957216.jpg) 此时有同学可能就疑惑了,那result[6] , result[7]怎么没更新啊,元素也一直在栈里。 @@ -515,7 +513,3 @@ impl Solution { -

- - - diff --git a/problems/0743.网络延迟时间.md b/problems/0743.网络延迟时间.md index e631951a..c8a87361 100644 --- a/problems/0743.网络延迟时间.md +++ b/problems/0743.网络延迟时间.md @@ -1,3 +1,6 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 743.网络延迟时间 @@ -10,7 +13,7 @@ https://leetcode.cn/problems/network-delay-time/description/ 现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240229104105.png) +![](https://file.kamacoder.com/pics/20240229104105.png) 提示: @@ -39,7 +42,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 如本题示例中的图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240125162647.png) +![](https://file.kamacoder.com/pics/20240125162647.png) 起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 @@ -85,7 +88,7 @@ minDist数组数值初始化为int最大值。 这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) +![](https://file.kamacoder.com/pics/20240130115306.png) (图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) @@ -107,7 +110,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) +![](https://file.kamacoder.com/pics/20240130115421.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -133,7 +136,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) +![](https://file.kamacoder.com/pics/20240130121240.png) 更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 @@ -167,7 +170,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) +![](https://file.kamacoder.com/pics/20240130120434.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -187,7 +190,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) +![](https://file.kamacoder.com/pics/20240201105335.png) 由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: @@ -207,7 +210,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110250.png) +![](https://file.kamacoder.com/pics/20240201110250.png) 由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: @@ -227,7 +230,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110651.png) +![](https://file.kamacoder.com/pics/20240201110651.png) 由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: @@ -245,7 +248,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110920.png) +![](https://file.kamacoder.com/pics/20240201110920.png) 节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 @@ -259,7 +262,7 @@ minDist数组数值初始化为int最大值。 路径如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) +![](https://file.kamacoder.com/pics/20240201111352.png) 在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 @@ -428,7 +431,7 @@ select:4 看一下这个图: (有负权值) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104334.png) +![](https://file.kamacoder.com/pics/20240227104334.png) 节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 @@ -438,7 +441,7 @@ select:4 初始化: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) +![](https://file.kamacoder.com/pics/20240227104801.png) --------------- @@ -452,7 +455,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110217.png) +![](https://file.kamacoder.com/pics/20240227110217.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -471,7 +474,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110330.png) +![](https://file.kamacoder.com/pics/20240227110330.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -489,7 +492,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110346.png) +![](https://file.kamacoder.com/pics/20240227110346.png) 由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: @@ -507,7 +510,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110405.png) +![](https://file.kamacoder.com/pics/20240227110405.png) 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 @@ -523,7 +526,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) +![](https://file.kamacoder.com/pics/20240227110711.png) -------------- @@ -651,7 +654,7 @@ for (int v = 1; v <= n; v++) { 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) +![](https://file.kamacoder.com/pics/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 @@ -675,7 +678,7 @@ for (int v = 1; v <= n; v++) { 邻接表的构造如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: @@ -760,7 +763,7 @@ vector> grid(n + 1); 不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 图中邻接表表示: @@ -781,7 +784,7 @@ vector>> grid(n + 1); 举例来给大家展示 该代码表达的数据 如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103904.png) +![](https://file.kamacoder.com/pics/20240223103904.png) * 节点1 指向 节点3 权值为 1 * 节点1 指向 节点5 权值为 2 @@ -904,7 +907,7 @@ for (int v = 1; v <= n; v++) { 再回顾一下邻接表的构造(数组 + 链表): -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 753a104d..147c7bfb 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -54,7 +52,7 @@ 请你计算并返回达到楼梯顶部的最低花费。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221031170131.png) +![](https://file.kamacoder.com/pics/20221031170131.png) ## 思路 @@ -114,7 +112,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。 拿示例2:cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] ,来模拟一下dp数组的状态变化,如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221026175104.png) +![](https://file.kamacoder.com/pics/20221026175104.png) 如果大家代码写出来有问题,就把dp数组打印出来,看看和如上推导的是不是一样的。 @@ -538,7 +536,3 @@ public class Solution -

- - - diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 19d862db..daf52bea 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 763.划分字母区间 @@ -46,7 +44,7 @@ 如图: -![763.划分字母区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201222191924417.png) +![763.划分字母区间](https://file.kamacoder.com/pics/20201222191924417.png) 明白原理之后,代码并不复杂,如下: @@ -462,7 +460,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0787.K站中转内最便宜的航班.md b/problems/0787.K站中转内最便宜的航班.md index 9c0a8e7f..6133ac77 100644 --- a/problems/0787.K站中转内最便宜的航班.md +++ b/problems/0787.K站中转内最便宜的航班.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 787. K 站中转内最便宜的航班 @@ -11,11 +9,11 @@ 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319103900.png) +![](https://file.kamacoder.com/pics/20240319103900.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319103919.png) +![](https://file.kamacoder.com/pics/20240319103919.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319104026.png) +![](https://file.kamacoder.com/pics/20240319104026.png) ## 思路 @@ -180,7 +178,3 @@ public: -

- - - diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index 40e1bbe7..639b6b2b 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 797.所有可能的路径 @@ -13,7 +11,7 @@ graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边)。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221203135439.png) +![](https://file.kamacoder.com/pics/20221203135439.png) 提示: @@ -98,7 +96,7 @@ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 一些录友可以疑惑这里如果找到x 链接的节点的,例如如果x目前是节点0,那么目前的过程就是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221204111937.png) +![](https://file.kamacoder.com/pics/20221204111937.png) 二维数组中,graph[x][i] 都是x链接的节点,当前遍历的节点就是 `graph[x][i]` 。 @@ -294,7 +292,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0827.最大人工岛.md b/problems/0827.最大人工岛.md index d24eaacc..e6aa4601 100644 --- a/problems/0827.最大人工岛.md +++ b/problems/0827.最大人工岛.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 827.最大人工岛 @@ -53,11 +51,11 @@ 拿如下地图的岛屿情况来举例: (1为陆地) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829104834.png) +![](https://file.kamacoder.com/pics/20220829104834.png) 第一步,则遍历题目,并将岛屿到编号和面积上的统计,过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105644.png) +![](https://file.kamacoder.com/pics/20220829105644.png) 本过程代码如下: @@ -104,7 +102,7 @@ int largestIsland(vector>& grid) { 第二步过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105249.png) +![](https://file.kamacoder.com/pics/20220829105249.png) 也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 @@ -504,7 +502,3 @@ return res; ``` -

- - - diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index b78693b4..60180d27 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -37,7 +35,7 @@ 图中给我的两个示例: `[[1],[2],[3],[]]` `[[1,3],[3,0,1],[2],[0]]`,画成对应的图如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220714101414.png) +![](https://file.kamacoder.com/pics/20220714101414.png) 我们可以看出图1的所有节点都是链接的,而图二中,节点2 是孤立的。 @@ -50,7 +48,7 @@ 图3:[[5], [], [1, 3], [5]] ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220714102201.png) +![](https://file.kamacoder.com/pics/20220714102201.png) 在图3中,大家可以发现,节点0只能到节点5,然后就哪也去不了了。 @@ -484,7 +482,3 @@ function canVisitAllRooms(rooms: number[][]): boolean { ``` -

- - - diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index c32cdd33..f2294473 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 844.比较含退格的字符串 @@ -588,7 +586,3 @@ impl Solution { ``` -

- - - diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index b7887d45..aeb470fe 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 860.柠檬水找零 @@ -440,7 +438,3 @@ public class Solution -

- - - diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md index 28680dbf..484099f8 100644 --- a/problems/0922.按奇偶排序数组II.md +++ b/problems/0922.按奇偶排序数组II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -410,7 +408,3 @@ function sortArrayByParityII(nums: number[]): number[] { ``` -

- - - diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index f4a8fa8e..47465199 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 925.长按键入 @@ -227,7 +225,3 @@ function isLongPressedName(name: string, typed: string): boolean { -

- - - diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index 77167df0..d4165f36 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 941.有效的山脉数组 @@ -18,7 +16,7 @@ * arr[0] < arr[1] < ... arr[i-1] < arr[i] * arr[i] > arr[i+1] > ... > arr[arr.length - 1] -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729103604.png) +![](https://file.kamacoder.com/pics/20210729103604.png) 示例 1: * 输入:arr = [2,1] @@ -213,7 +211,3 @@ impl Solution { } ``` -

- - - diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index 327c54f7..d8c31ca9 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -19,7 +17,7 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229175736596.png) +![](https://file.kamacoder.com/pics/20201229175736596.png) * 输入:[0,0,null,0,0] * 输出:1 @@ -27,7 +25,7 @@ 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/2020122917584449.png) +![](https://file.kamacoder.com/pics/2020122917584449.png) * 输入:[0,0,null,0,null,0,null,null,0] * 输出:2 @@ -145,7 +143,7 @@ if (cur == NULL) return 2; 如图: -![968.监控二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229203710729.png) +![968.监控二叉树2](https://file.kamacoder.com/pics/20201229203710729.png) 代码如下: @@ -193,7 +191,7 @@ if (left == 1 || right == 1) return 2; **从这个代码中,可以看出,如果left == 1, right == 0 怎么办?其实这种条件在情况2中已经判断过了**,如图: -![968.监控二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020122920362355.png) +![968.监控二叉树1](https://file.kamacoder.com/pics/2020122920362355.png) 这种情况也是大多数同学容易迷惑的情况。 @@ -201,7 +199,7 @@ if (left == 1 || right == 1) return 2; 以上都处理完了,递归结束之后,可能头结点 还有一个无覆盖的情况,如图: -![968.监控二叉树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229203742446.png) +![968.监控二叉树3](https://file.kamacoder.com/pics/20201229203742446.png) 所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下: @@ -789,7 +787,3 @@ public class Solution } ``` -

- - - diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index b8488e10..6e58be1a 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 双指针风骚起来,也是无敌 @@ -588,8 +586,4 @@ public class Solution { } ``` -

- - - diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index f938c2b7..3d7d8e07 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -582,7 +580,3 @@ end ``` -

- - - diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 1a781777..6e908d5a 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1005.K次取反后最大化的数组和 @@ -377,7 +375,3 @@ public class Solution -

- - - diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md index f708e4a3..ae6b3895 100644 --- a/problems/1020.飞地的数量.md +++ b/problems/1020.飞地的数量.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1020. 飞地的数量 @@ -14,13 +12,13 @@ 返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830100710.png) +![](https://file.kamacoder.com/pics/20220830100710.png) * 输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] * 输出:3 * 解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830100742.png) +![](https://file.kamacoder.com/pics/20220830100742.png) * 输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] * 输出:0 @@ -34,11 +32,11 @@ 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104632.png) +![](https://file.kamacoder.com/pics/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104651.png) +![](https://file.kamacoder.com/pics/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 @@ -753,8 +751,4 @@ impl Solution { -

- - - diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index 5164e1f7..0119df82 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1035.不相交的线 @@ -20,7 +18,7 @@ 以这种方法绘制线条,并返回可以绘制的最大连线数。 -![1035.不相交的线](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032116363533.png) +![1035.不相交的线](https://file.kamacoder.com/pics/2021032116363533.png) ## 算法公开课 @@ -38,7 +36,7 @@ 拿示例一nums1 = [1,4,2], nums2 = [1,2,4]为例,相交情况如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914145158.png) +![](https://file.kamacoder.com/pics/20210914145158.png) 其实也就是说nums1和nums2的最长公共子序列是[1,4],长度为2。 这个公共子序列指的是相对顺序不变(即数字4在字符串nums1中数字1的后面,那么数字4也应该在字符串nums2数字1的后面) @@ -278,7 +276,3 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { -

- - - diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 51ec4e62..01d33fbf 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 匹配问题都是栈的强项 @@ -523,7 +521,3 @@ end ``` -

- - - diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index 0d445a71..6dfba4ed 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1049.最后一块石头的重量II @@ -116,7 +114,7 @@ for (int i = 0; i < stones.size(); i++) { // 遍历物品 举例,输入:[2,4,1,1],此时target = (2 + 4 + 1 + 1)/2 = 4 ,dp数组状态图如下: -![1049.最后一块石头的重量II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121115805904.jpg) +![1049.最后一块石头的重量II](https://file.kamacoder.com/pics/20210121115805904.jpg) 最后dp[target]里是容量为target的背包所能背的最大重量。 @@ -535,7 +533,3 @@ public class Solution ``` -

- - - diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index 6d05ccf3..91c29b83 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1143.最长公共子序列 @@ -96,7 +94,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 从递推公式,可以看出,有三个方向可以推出dp[i][j],如图: -![1143.最长公共子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204115139616.jpg) +![1143.最长公共子序列](https://file.kamacoder.com/pics/20210204115139616.jpg) 那么为了在递推的过程中,这三个方向都是经过计算的数值,所以要从前向后,从上到下来遍历这个矩阵。 @@ -105,7 +103,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 以输入:text1 = "abcde", text2 = "ace" 为例,dp状态如图: -![1143.最长公共子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210210150215918.jpg) +![1143.最长公共子序列1](https://file.kamacoder.com/pics/20210210150215918.jpg) 最后红框dp[text1.size()][text2.size()]为最终结果 @@ -420,7 +418,3 @@ func longestCommonSubsequence(text1: String, text2: String): Int64 { ``` -

- - - diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index 781badf5..fbb19af7 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1207.独一无二的出现次数 @@ -247,7 +245,3 @@ impl Solution { -

- - - diff --git a/problems/1221.分割平衡字符串.md b/problems/1221.分割平衡字符串.md index a32ca98f..a9e275d9 100644 --- a/problems/1221.分割平衡字符串.md +++ b/problems/1221.分割平衡字符串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1221. 分割平衡字符串 @@ -172,7 +170,3 @@ function balancedStringSplit(s: string): number { }; ``` -

- - - diff --git a/problems/1254.统计封闭岛屿的数目.md b/problems/1254.统计封闭岛屿的数目.md index 3d7b9fe9..ebea30e3 100644 --- a/problems/1254.统计封闭岛屿的数目.md +++ b/problems/1254.统计封闭岛屿的数目.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1254. 统计封闭岛屿的数目 @@ -12,13 +10,13 @@ 请返回 封闭岛屿 的数目。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830111533.png) +![](https://file.kamacoder.com/pics/20220830111533.png) * 输入:grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]] * 输出:2 * 解释:灰色区域的岛屿是封闭岛屿,因为这座岛屿完全被水域包围(即被 1 区域包围)。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830111601.png) +![](https://file.kamacoder.com/pics/20220830111601.png) * 输入:grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]] * 输出:1 @@ -136,7 +134,3 @@ var closedIsland = function(grid) { -

- - - diff --git a/problems/1334.阈值距离内邻居最少的城市.md b/problems/1334.阈值距离内邻居最少的城市.md index d8d8861f..bea47a2e 100644 --- a/problems/1334.阈值距离内邻居最少的城市.md +++ b/problems/1334.阈值距离内邻居最少的城市.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) floyd @@ -49,7 +47,3 @@ public: return result; } }; -

- - - diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index 9cfb6743..0ae16034 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -217,7 +215,3 @@ var sortByBits = function(arr) { -

- - - diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index f0a77f55..2cb73f72 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -311,7 +309,3 @@ impl Solution { ``` -

- - - diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index 120cafff..7b0d3204 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1382.将二叉搜索树变平衡 @@ -17,7 +15,7 @@ 示例: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726154512.png) +![](https://file.kamacoder.com/pics/20210726154512.png) * 输入:root = [1,null,2,null,3,null,4,null,null] * 输出:[2,1,3,null,null,null,4] @@ -218,7 +216,3 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null -

- - - diff --git a/problems/1791.找出星型图的中心节点.md b/problems/1791.找出星型图的中心节点.md index e3db7947..5dd56c65 100644 --- a/problems/1791.找出星型图的中心节点.md +++ b/problems/1791.找出星型图的中心节点.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1791.找出星型图的中心节点 @@ -12,7 +10,7 @@ 什么是度,可以理解为,链接节点的边的数量。 题目中度如图所示: -![1791.找出星型图的中心节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20220704113207.png) +![1791.找出星型图的中心节点](https://file.kamacoder.com/pics/20220704113207.png) 至于出度和入度,那就是在有向图里的概念了,本题是无向图。 @@ -77,7 +75,3 @@ public: ``` -

- - - diff --git a/problems/1971.寻找图中是否存在路径.md b/problems/1971.寻找图中是否存在路径.md index 93e9b663..33b48698 100644 --- a/problems/1971.寻找图中是否存在路径.md +++ b/problems/1971.寻找图中是否存在路径.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1971. 寻找图中是否存在路径 @@ -14,7 +12,7 @@ 给你数组 edges 和整数 n、start 和 end,如果从 start 到 end 存在 有效路径 ,则返回 true,否则返回 false 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220705101442.png) +![](https://file.kamacoder.com/pics/20220705101442.png) @@ -335,7 +333,3 @@ func validPath(n int, edges [][]int, source int, destination int) bool { ``` -

- - - diff --git a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md index a5dab942..830bba7e 100644 --- a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md +++ b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 程序提交之后为什么会超时?O(n)的算法会超时,n究竟是多大? @@ -15,7 +13,7 @@ ## 超时是怎么回事 -![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) +![程序超时](https://file.kamacoder.com/pics/20200729112716117.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -131,11 +129,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460.png) +![程序超时2](https://file.kamacoder.com/pics/20200729200018460.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下O(n^2) 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970.png) +![程序超时3](https://file.kamacoder.com/pics/2020072919590970.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -143,7 +141,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 O(n)少一个数量级,因为logn的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407.png) +![程序超时4](https://file.kamacoder.com/pics/20200729195729407.png) O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -151,7 +149,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 **整体测试数据整理如下:** -![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175.png) +![程序超时1](https://file.kamacoder.com/pics/20201208231559175.png) 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 @@ -224,7 +222,3 @@ int main() { -

- - - diff --git a/problems/images/0110.字符串接龙-03.png b/problems/images/0110.字符串接龙-03.png new file mode 100644 index 00000000..7ad2ced6 Binary files /dev/null and b/problems/images/0110.字符串接龙-03.png differ diff --git a/problems/images/test b/problems/images/test new file mode 100644 index 00000000..e69de29b diff --git a/problems/kamacoder/0047.参会dijkstra堆.md b/problems/kamacoder/0047.参会dijkstra堆.md index 75c12f8a..e361e8e0 100644 --- a/problems/kamacoder/0047.参会dijkstra堆.md +++ b/problems/kamacoder/0047.参会dijkstra堆.md @@ -46,13 +46,13 @@ 如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101345.png) +![](https://file.kamacoder.com/pics/20240227101345.png) 不能到达的情况: 如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101401.png) +![](https://file.kamacoder.com/pics/20240227101401.png) 数据范围: @@ -101,7 +101,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) +![](https://file.kamacoder.com/pics/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 @@ -125,7 +125,7 @@ 邻接表的构造如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: @@ -210,7 +210,7 @@ vector> grid(n + 1); 不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 图中邻接表表示: @@ -231,7 +231,7 @@ vector>> grid(n + 1); 举例来给大家展示 该代码表达的数据 如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103904.png) +![](https://file.kamacoder.com/pics/20240223103904.png) * 节点1 指向 节点3 权值为 1 * 节点1 指向 节点5 权值为 2 @@ -354,7 +354,7 @@ for (int v = 1; v <= n; v++) { 再回顾一下邻接表的构造(数组 + 链表): -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) diff --git a/problems/kamacoder/0047.参会dijkstra朴素.md b/problems/kamacoder/0047.参会dijkstra朴素.md index e71e9d53..1ff9f1a8 100644 --- a/problems/kamacoder/0047.参会dijkstra朴素.md +++ b/problems/kamacoder/0047.参会dijkstra朴素.md @@ -46,13 +46,13 @@ 如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101345.png) +![](https://file.kamacoder.com/pics/20240227101345.png) 不能到达的情况: 如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101401.png) +![](https://file.kamacoder.com/pics/20240227101401.png) 数据范围: @@ -76,7 +76,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 如本题示例中的图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240125162647.png) +![](https://file.kamacoder.com/pics/20240125162647.png) 起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 @@ -122,7 +122,7 @@ minDist数组数值初始化为int最大值。 这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) +![](https://file.kamacoder.com/pics/20240130115306.png) (图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) @@ -144,7 +144,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) +![](https://file.kamacoder.com/pics/20240130115421.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -170,7 +170,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) +![](https://file.kamacoder.com/pics/20240130121240.png) 更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 @@ -204,7 +204,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) +![](https://file.kamacoder.com/pics/20240130120434.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -224,7 +224,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) +![](https://file.kamacoder.com/pics/20240201105335.png) 由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: @@ -244,7 +244,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110250.png) +![](https://file.kamacoder.com/pics/20240201110250.png) 由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: @@ -264,7 +264,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110651.png) +![](https://file.kamacoder.com/pics/20240201110651.png) 由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: @@ -282,7 +282,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110920.png) +![](https://file.kamacoder.com/pics/20240201110920.png) 节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 @@ -296,7 +296,7 @@ minDist数组数值初始化为int最大值。 路径如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) +![](https://file.kamacoder.com/pics/20240201111352.png) 在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 @@ -541,7 +541,7 @@ int main() { 对应如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) +![](https://file.kamacoder.com/pics/20240201111352.png) ### 出现负数 @@ -549,7 +549,7 @@ int main() { 看一下这个图: (有负权值) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104334.png) +![](https://file.kamacoder.com/pics/20240227104334.png) 节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 @@ -559,7 +559,7 @@ int main() { 初始化: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) +![](https://file.kamacoder.com/pics/20240227104801.png) --------------- @@ -573,7 +573,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110217.png) +![](https://file.kamacoder.com/pics/20240227110217.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -592,7 +592,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110330.png) +![](https://file.kamacoder.com/pics/20240227110330.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -610,7 +610,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110346.png) +![](https://file.kamacoder.com/pics/20240227110346.png) 由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: @@ -628,7 +628,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110405.png) +![](https://file.kamacoder.com/pics/20240227110405.png) 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 @@ -644,7 +644,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) +![](https://file.kamacoder.com/pics/20240227110711.png) -------------- diff --git a/problems/kamacoder/0053.寻宝-Kruskal.md b/problems/kamacoder/0053.寻宝-Kruskal.md index 861efe68..585fa476 100644 --- a/problems/kamacoder/0053.寻宝-Kruskal.md +++ b/problems/kamacoder/0053.寻宝-Kruskal.md @@ -63,7 +63,7 @@ kruscal的思路: 依然以示例中,如下这个图来举例。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111113514.png) +![](https://file.kamacoder.com/pics/20240111113514.png) 将图中的边按照权值有小到大排序,这样从贪心的角度来说,优先选 权值小的边加入到 最小生成树中。 @@ -77,13 +77,13 @@ kruscal的思路: 选边(1,2),节点1 和 节点2 不在同一个集合,所以生成树可以添加边(1,2),并将 节点1,节点2 放在同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111114204.png) +![](https://file.kamacoder.com/pics/20240111114204.png) -------- 选边(4,5),节点4 和 节点 5 不在同一个集合,生成树可以添加边(4,5) ,并将节点4,节点5 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111120458.png) +![](https://file.kamacoder.com/pics/20240111120458.png) **大家判断两个节点是否在同一个集合,就看图中两个节点是否有绿色的粗线连着就行** @@ -93,25 +93,25 @@ kruscal的思路: 选边(1,3),节点1 和 节点3 不在同一个集合,生成树添加边(1,3),并将节点1,节点3 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112105834.png) +![](https://file.kamacoder.com/pics/20240112105834.png) --------- 选边(2,6),节点2 和 节点6 不在同一个集合,生成树添加边(2,6),并将节点2,节点6 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110214.png) +![](https://file.kamacoder.com/pics/20240112110214.png) -------- 选边(3,4),节点3 和 节点4 不在同一个集合,生成树添加边(3,4),并将节点3,节点4 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110450.png) +![](https://file.kamacoder.com/pics/20240112110450.png) ---------- 选边(6,7),节点6 和 节点7 不在同一个集合,生成树添加边(6,7),并将 节点6,节点7 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) +![](https://file.kamacoder.com/pics/20240112110637.png) ----------- @@ -126,7 +126,7 @@ kruscal的思路: 此时 我们就已经生成了一个最小生成树,即: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) +![](https://file.kamacoder.com/pics/20240112110637.png) 在上面的讲解中,看图的话 大家知道如何判断 两个节点 是否在同一个集合(是否有绿色的线连在一起),以及如何把两个节点加入集合(就在图中把两个节点连上) @@ -346,7 +346,7 @@ int main() { 大家可能发现 怎么和我们 模拟画的图不一样,差别在于 代码生成的最小生成树中 节点5 和 节点7相连的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240116163014.png) +![](https://file.kamacoder.com/pics/20240116163014.png) 其实造成这个差别 是对边排序的时候 权值相同的边先后顺序的问题导致的,无论相同权值边的顺序是什么样的,最后都能得出最小生成树。 @@ -366,7 +366,7 @@ Kruskal 与 prim 的关键区别在于,prim维护的是节点的集合,而 K 节点未必一定要连着边那, 例如 这个图,大家能明显感受到边没有那么多对吧,但节点数量 和 上述我们讲的例子是一样的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240116152211.png) +![](https://file.kamacoder.com/pics/20240116152211.png) 为什么边少的话,使用 Kruskal 更优呢? diff --git a/problems/kamacoder/0053.寻宝-prim.md b/problems/kamacoder/0053.寻宝-prim.md index 8e26bea4..a7d35841 100644 --- a/problems/kamacoder/0053.寻宝-prim.md +++ b/problems/kamacoder/0053.寻宝-prim.md @@ -61,7 +61,7 @@ 例如本题示例中的无向有权图为: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231206164306.png) +![](https://file.kamacoder.com/pics/20231206164306.png) 那么在这个图中,如何选取n-1条边使得图中所有节点连接到一起,并且边的权值和最小呢? @@ -100,7 +100,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231215105603.png) +![](https://file.kamacoder.com/pics/20231215105603.png) 开始构造最小生成树 @@ -118,7 +118,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来,我们要更新所有节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102048.png) +![](https://file.kamacoder.com/pics/20231222102048.png) 注意下标0,我们就不管它了,下标1与节点1对应,这样可以避免大家把节点搞混。 @@ -148,7 +148,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来,我们要更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102431.png) +![](https://file.kamacoder.com/pics/20231222102431.png) 此时所有非生成树的节点距离最小生成树(节点1、节点2)的距离都已经跟新了。 @@ -172,7 +172,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102457.png) +![](https://file.kamacoder.com/pics/20231222102457.png) 所有非生成树的节点距离最小生成树(节点1、节点2、节点3)的距离都已经跟新了。 @@ -188,7 +188,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 继续选择一个距离最小生成树(节点1、节点2、节点3)最近的非生成树里的节点,为了巩固大家对minDist数组的理解,这里我再啰嗦一遍: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231217213516.png) +![](https://file.kamacoder.com/pics/20231217213516.png) **minDist数组是记录了所有非生成树节点距离生成树的最小距离**,所以从数组里我们能看出来,非生成树节点4和节点6距离生成树最近。 @@ -209,7 +209,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102618.png) +![](https://file.kamacoder.com/pics/20231222102618.png) minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4)的距离。 @@ -232,7 +232,7 @@ minDist数组已经更新了所有非生成树的节点距离最小生成树( 接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102646.png) +![](https://file.kamacoder.com/pics/20231222102646.png) minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4、节点5)的距离。 @@ -253,11 +253,11 @@ minDist数组已经更新了所有非生成树的节点距离最小生成树( 节点1、节点2、节点3、节点4、节点5、节点6算是最小生成树的节点,接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102732.png) +![](https://file.kamacoder.com/pics/20231222102732.png) 这里就不在重复描述了,大家类推,最后,节点7加入生成树,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102820.png) +![](https://file.kamacoder.com/pics/20231222102820.png) ### 最后 @@ -478,7 +478,7 @@ int main() { 大家可以和我们本题最后生成的最小生成树的图去对比一下边的链接情况: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231229115714.png) +![](https://file.kamacoder.com/pics/20231229115714.png) 绿色的边是最小生成树,和我们的输出完全一致。 diff --git a/problems/kamacoder/0054.替换数字.md b/problems/kamacoder/0054.替换数字.md index de0ab1a3..665e8ecb 100644 --- a/problems/kamacoder/0054.替换数字.md +++ b/problems/kamacoder/0054.替换数字.md @@ -21,7 +21,7 @@ ## 思路 -如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java刷题的录友,一定要使用辅助空间,因为Java里的string不能修改) +如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java和Python刷题的录友,一定要使用辅助空间,因为Java和Python里的string不能修改) 首先扩充数组到每个数字字符替换成 "number" 之后的大小。 @@ -29,11 +29,11 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030165201.png) +![](https://file.kamacoder.com/pics/20231030165201.png) 然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030173058.png) +![](https://file.kamacoder.com/pics/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? @@ -215,6 +215,46 @@ public class Main { } ``` +### Python: +```python +class Solution(object): + def subsitute_numbers(self, s): + """ + :type s: str + :rtype: str + """ + + count = sum(1 for char in s if char.isdigit()) # 统计数字的个数 + expand_len = len(s) + (count * 5) # 计算扩充后字符串的大小, x->number, 每有一个数字就要增加五个长度 + res = [''] * expand_len + + new_index = expand_len - 1 # 指向扩充后字符串末尾 + old_index = len(s) - 1 # 指向原字符串末尾 + + while old_index >= 0: # 从后往前, 遇到数字替换成“number” + if s[old_index].isdigit(): + res[new_index-5:new_index+1] = "number" + new_index -= 6 + else: + res[new_index] = s[old_index] + new_index -= 1 + old_index -= 1 + + return "".join(res) + +if __name__ == "__main__": + solution = Solution() + + while True: + try: + s = input() + result = solution.subsitute_numbers(s) + print(result) + except EOFError: + break + +``` + ### Go: ````go package main diff --git a/problems/kamacoder/0055.右旋字符串.md b/problems/kamacoder/0055.右旋字符串.md index 2b32cb44..48150222 100644 --- a/problems/kamacoder/0055.右旋字符串.md +++ b/problems/kamacoder/0055.右旋字符串.md @@ -40,16 +40,16 @@ fgabcde 本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106170143.png) +![](https://file.kamacoder.com/pics/20231106170143.png) 右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106171557.png) +![](https://file.kamacoder.com/pics/20231106171557.png) 此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172058.png) +![](https://file.kamacoder.com/pics/20231106172058.png) 其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 @@ -80,7 +80,7 @@ int main() { 可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172534.png) +![](https://file.kamacoder.com/pics/20231106172534.png) 代码如下: diff --git a/problems/kamacoder/0058.区间和.md b/problems/kamacoder/0058.区间和.md index 23e7189a..8eaad9f0 100644 --- a/problems/kamacoder/0058.区间和.md +++ b/problems/kamacoder/0058.区间和.md @@ -93,7 +93,7 @@ int main() { 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627110604.png) +![](https://file.kamacoder.com/pics/20240627110604.png) 如果,我们想统计,在vec数组上 下标 2 到下标 5 之间的累加和,那是不是就用 p[5] - p[1] 就可以了。 @@ -109,7 +109,7 @@ int main() { 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627111319.png) +![](https://file.kamacoder.com/pics/20240627111319.png) `p[5] - p[1]` 就是 红色部分的区间和。 @@ -357,3 +357,54 @@ int main(int argc, char *argv[]) ``` +### Go + +```go +package main + +import ( + "fmt" + "bufio" + "strconv" + "os" +) + +func main() { + // bufio中读取数据的接口,因为数据卡的比较严,导致使用fmt.Scan会超时 + scanner := bufio.NewScanner(os.Stdin) + + // 获取数组大小 + scanner.Scan() + n, _ := strconv.Atoi(scanner.Text()) + + // 获取数组元素的同时计算前缀和,一般建议切片开大一点防止各种越界问题 + arr := make([]int, n + 1) + for i := 0; i < n; i++ { + scanner.Scan() + arr[i], _ = strconv.Atoi(scanner.Text()) + if i != 0 { + arr[i] += arr[i - 1] + } + } + + /* + 区间[l, r]的和可以使用区间[0, r]和[0, l - 1]相减得到, + 在代码中即为arr[r]-arr[l-1]。这里需要注意l-1是否越界 + */ + for { + var l, r int + scanner.Scan() + _, err := fmt.Sscanf(scanner.Text(), "%d %d", &l, &r) + if err != nil { + return + } + + if l > 0 { + fmt.Println(arr[r] - arr[l - 1]) + } else { + fmt.Println(arr[r]) + } + } +} +``` + diff --git a/problems/kamacoder/0094.城市间货物运输I-SPFA.md b/problems/kamacoder/0094.城市间货物运输I-SPFA.md index 99986aaa..b5920292 100644 --- a/problems/kamacoder/0094.城市间货物运输I-SPFA.md +++ b/problems/kamacoder/0094.城市间货物运输I-SPFA.md @@ -62,7 +62,7 @@ 给大家举一个例子: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) +![](https://file.kamacoder.com/pics/20240328104119.png) 本图中,对所有边进行松弛,真正有效的松弛,只有松弛 边(节点1->节点2) 和 边(节点1->节点3) 。 @@ -97,7 +97,7 @@ 初始化,起点为节点1, 起点到起点的最短距离为0,所以minDist[1] 为 0。 将节点1 加入队列 (下次松弛从节点1开始) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115555.png) +![](https://file.kamacoder.com/pics/20240411115555.png) ------------ @@ -109,7 +109,7 @@ 将节点2、节点3 加入队列,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115544.png) +![](https://file.kamacoder.com/pics/20240411115544.png) ----------------- @@ -124,7 +124,7 @@ 将节点4,节点5 加入队列,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110348.png) +![](https://file.kamacoder.com/pics/20240412110348.png) -------------------- @@ -134,7 +134,7 @@ 因为没有从节点3作为出发点的边,所以这里就从队列里取出节点3就好,不用做其他操作,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110420.png) +![](https://file.kamacoder.com/pics/20240412110420.png) ------------ @@ -147,7 +147,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110445.png) +![](https://file.kamacoder.com/pics/20240412110445.png) --------------- @@ -160,7 +160,7 @@ 如图,将节点3加入队列,因为节点6已经在队列里,所以不用重复添加 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240729161116.png) +![](https://file.kamacoder.com/pics/20240729161116.png) 所以我们在加入队列的过程可以有一个优化,**用visited数组记录已经在队列里的元素,已经在队列的元素不用重复加入** @@ -174,7 +174,7 @@ 所以直接从队列中取出,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115424.png) +![](https://file.kamacoder.com/pics/20240411115424.png) ---------- @@ -264,7 +264,7 @@ int main() { 至于为什么 双向图且每一个节点和所有其他节点都相连的话,每个节点 都有 n-1 条指向该节点的边, 我再来举个例子,如图: -[](https://code-thinking-1253855093.file.myqcloud.com/pics/20240416104138.png) +[](https://file.kamacoder.com/pics/20240416104138.png) 图中 每个节点都与其他所有节点相连,节点数n 为 4,每个节点都有3条指向该节点的边,即入度为3。 @@ -329,7 +329,7 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412111849.png) +![](https://file.kamacoder.com/pics/20240412111849.png) 正权回路 就是有环,但环的总权值为正数。 diff --git a/problems/kamacoder/0094.城市间货物运输I.md b/problems/kamacoder/0094.城市间货物运输I.md index 2afc014b..9edde8ac 100644 --- a/problems/kamacoder/0094.城市间货物运输I.md +++ b/problems/kamacoder/0094.城市间货物运输I.md @@ -46,7 +46,7 @@ 1 3 5 ``` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240509200224.png) +![](https://file.kamacoder.com/pics/20240509200224.png) ## 思路 @@ -78,7 +78,7 @@ 这里我给大家举一个例子,每条边有起点、终点和边的权值。例如一条边,节点A 到 节点B 权值为value,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240327102620.png) +![](https://file.kamacoder.com/pics/20240327102620.png) minDist[B] 表示 到达B节点 最小权值,minDist[B] 有哪些状态可以推出来? @@ -127,7 +127,7 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) +![](https://file.kamacoder.com/pics/20240328104119.png) 其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 @@ -150,36 +150,36 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 边:节点5 -> 节点6,权值为-2 ,minDist[5] 还是默认数值max,所以不能基于 节点5 去更新节点6,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113537.png) +![](https://file.kamacoder.com/pics/20240329113537.png) (在复习一下,minDist[5] 表示起点到节点5的最短距离) 边:节点1 -> 节点2,权值为1 ,minDist[2] > minDist[1] + 1 ,更新 minDist[2] = minDist[1] + 1 = 0 + 1 = 1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113703.png) +![](https://file.kamacoder.com/pics/20240329113703.png) 边:节点5 -> 节点3,权值为1 ,minDist[5] 还是默认数值max,所以不能基于节点5去更新节点3 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113827.png) +![](https://file.kamacoder.com/pics/20240329113827.png) 边:节点2 -> 节点5,权值为2 ,minDist[5] > minDist[2] + 2 (经过上面的计算minDist[2]已经不是默认值,而是 1),更新 minDist[5] = minDist[2] + 2 = 1 + 2 = 3 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113927.png) +![](https://file.kamacoder.com/pics/20240329113927.png) 边:节点2 -> 节点4,权值为-3 ,minDist[4] > minDist[2] + (-3),更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114036.png) +![](https://file.kamacoder.com/pics/20240329114036.png) 边:节点4 -> 节点6,权值为4 ,minDist[6] > minDist[4] + 4,更新 minDist[6] = minDist[4] + 4 = -2 + 4 = 2 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114120.png) +![](https://file.kamacoder.com/pics/20240329114120.png) 边:节点1 -> 节点3,权值为5 ,minDist[3] > minDist[1] + 5,更新 minDist[3] = minDist[1] + 5 = 0 + 5 = 5 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114324.png) +![](https://file.kamacoder.com/pics/20240329114324.png) -------- diff --git a/problems/kamacoder/0095.城市间货物运输II.md b/problems/kamacoder/0095.城市间货物运输II.md index a3896b88..5dddf450 100644 --- a/problems/kamacoder/0095.城市间货物运输II.md +++ b/problems/kamacoder/0095.城市间货物运输II.md @@ -78,7 +78,7 @@ circle 我们拿题目中示例来画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240705161426.png) +![](https://file.kamacoder.com/pics/20240705161426.png) 图中 节点1 到 节点4 的最短路径是多少(题目中的最低运输成本) (注意边可以为负数的) @@ -86,7 +86,7 @@ circle 而图中有负权回路: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402103712.png) +![](https://file.kamacoder.com/pics/20240402103712.png) 那么我们在负权回路中多绕一圈,我们的最短路径 是不是就更小了 (也就是更低的运输成本) diff --git a/problems/kamacoder/0096.城市间货物运输III.md b/problems/kamacoder/0096.城市间货物运输III.md index 0e13846d..37cfaee0 100644 --- a/problems/kamacoder/0096.城市间货物运输III.md +++ b/problems/kamacoder/0096.城市间货物运输III.md @@ -63,7 +63,7 @@ 本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402115614.png) +![](https://file.kamacoder.com/pics/20240402115614.png) 图中,节点1 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 @@ -195,7 +195,7 @@ int main() { 起点为节点1, 起点到起点的距离为0,所以 minDist[1] 初始化为0 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111940.png) +![](https://file.kamacoder.com/pics/20240409111940.png) 其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 @@ -203,21 +203,21 @@ int main() { 边:节点1 -> 节点2,权值为-1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = minDist[1] + (-1) = 0 - 1 = -1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111914.png) +![](https://file.kamacoder.com/pics/20240409111914.png) 边:节点2 -> 节点3,权值为1 ,minDist[3] > minDist[2] + 1 ,更新 minDist[3] = minDist[2] + 1 = -1 + 1 = 0 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111903.png) +![](https://file.kamacoder.com/pics/20240409111903.png) 边:节点3 -> 节点1,权值为-1 ,minDist[1] > minDist[3] + (-1),更新 minDist[1] = 0 + (-1) = -1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111849.png) +![](https://file.kamacoder.com/pics/20240409111849.png) 边:节点3 -> 节点4,权值为1 ,minDist[4] > minDist[3] + 1,更新 minDist[4] = 0 + 1 = 1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241018192042.png) +![](https://file.kamacoder.com/pics/20241018192042.png) 以上是对所有边进行的第一次松弛,最后 minDist数组为 :-1 -1 0 1 ,(从下标1算起) @@ -244,7 +244,7 @@ int main() { 在上面画图距离中,对所有边进行第一次松弛,在计算 边(节点2 -> 节点3) 的时候,更新了 节点3。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111903.png) +![](https://file.kamacoder.com/pics/20240409111903.png) 理论上来说节点3 应该在对所有边第二次松弛的时候才更新。 这因为当时是基于已经计算好的 节点2(minDist[2])来做计算了。 @@ -331,11 +331,11 @@ int main() { 所构成是图是一样的,都是如下的这个图,但给出的边的顺序是不一样的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410154340.png) +![](https://file.kamacoder.com/pics/20240410154340.png) 再用版本一的代码是运行一下,发现结果输出是 1, 是对的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410154940.png) +![](https://file.kamacoder.com/pics/20240410154940.png) 分明刚刚输出的结果是 -2,是错误的,怎么 一样的图,这次输出的结果就对了呢? @@ -345,7 +345,7 @@ int main() { 初始化: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410155545.png) +![](https://file.kamacoder.com/pics/20240410155545.png) 边:节点3 -> 节点1,权值为-1 ,节点3还没有被计算过,节点1 不更新。 @@ -355,7 +355,7 @@ int main() { 边:节点1 -> 节点2,权值为 -1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = 0 + (-1) = -1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410160046.png) +![](https://file.kamacoder.com/pics/20240410160046.png) 以上是对所有边 松弛一次的状态。 @@ -387,8 +387,6 @@ int main() { * 本题可以有负权回路,说明只要多做松弛,结果是会变的。 * 本题要求最多经过k个节点,对松弛次数是有限制的。 -如果本题中 没有负权回路的测试用例, 那版本一的代码就可以过了,也就不用我费这么大口舌去讲解的这个坑了。 - ## 拓展三(SPFA) 本题也可以用 SPFA来做,关于 SPFA ,已经在这里 [0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA.md) 有详细讲解。 @@ -474,7 +472,7 @@ int main() { 但大家会发现,以上代码大家提交后,怎么耗时这么多? -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418113308.png) +![](https://file.kamacoder.com/pics/20240418113308.png) 理论上,SPFA的时间复杂度不是要比 bellman_ford 更优吗? @@ -556,7 +554,7 @@ int main() { 以上代码提交后,耗时情况: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418113952.png) +![](https://file.kamacoder.com/pics/20240418113952.png) 大家发现 依然远比 bellman_ford 的代码版本 耗时高。 @@ -581,11 +579,11 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 在以下这个图中,求节点1 到 节点7 最多经过2个节点 的最短路是多少呢? -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508112249.png) +![](https://file.kamacoder.com/pics/20240508112249.png) 最短路显然是: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508112416.png) +![](https://file.kamacoder.com/pics/20240508112416.png) 最多经过2个节点,也就是3条边相连的路线:节点1 -> 节点2 -> 节点6-> 节点7 @@ -593,24 +591,24 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 初始化如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) +![](https://file.kamacoder.com/pics/20240130115306.png) 找距离源点最近且没有被访问过的节点,先找节点1 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) +![](https://file.kamacoder.com/pics/20240130115421.png) 距离源点最近且没有被访问过的节点,找节点2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) +![](https://file.kamacoder.com/pics/20240130121240.png) 距离源点最近且没有被访问过的节点,找到节点3: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) +![](https://file.kamacoder.com/pics/20240130120434.png) 距离源点最近且没有被访问过的节点,找到节点4: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) +![](https://file.kamacoder.com/pics/20240201105335.png) 此时最多经过2个节点的搜索就完毕了,但结果中minDist[7] (即节点7的结果)并没有被更。 diff --git a/problems/kamacoder/0097.小明逛公园.md b/problems/kamacoder/0097.小明逛公园.md index dfbd6aa9..97765ebc 100644 --- a/problems/kamacoder/0097.小明逛公园.md +++ b/problems/kamacoder/0097.小明逛公园.md @@ -155,7 +155,7 @@ grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点 grid数组是一个三维数组,那么我们初始化的数据在 i 与 j 构成的平层,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240425104247.png) +![](https://file.kamacoder.com/pics/20240425104247.png) 红色的 底部一层是我们初始化好的数据,注意:从三维角度去看初始化的数据很重要,下面我们在聊遍历顺序的时候还会再讲。 @@ -202,7 +202,7 @@ vector>> grid(n + 1, vector>(n + 1, vector(n 所以遍历k 的for循环一定是在最外面,这样才能一层一层去遍历。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120109.png) +![](https://file.kamacoder.com/pics/20240424120109.png) 至于遍历 i 和 j 的话,for 循环的先后顺序无所谓。 @@ -234,7 +234,7 @@ for (int i = 1; i <= n; i++) { 此时就遍历了 j 与 k 形成一个平面,i 则是纵面,那遍历 就是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) +![](https://file.kamacoder.com/pics/20240424115827.png) 而我们初始化的数据 是 k 为0, i 和 j 形成的平面做初始化,如果以 k 和 j 形成的平面去一层一层遍历,就造成了 递推公式 用不上上一轮计算的结果,从而导致结果不对(初始化的部分是 i 与j 形成的平面,在初始部分有讲过)。 @@ -253,7 +253,7 @@ for (int i = 1; i <= n; i++) { 就是图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120942.png) +![](https://file.kamacoder.com/pics/20240424120942.png) 求节点1 到 节点 2 的最短距离,运行结果是 10 ,但正确的结果很明显是3。 @@ -267,7 +267,7 @@ for (int i = 1; i <= n; i++) { 而遍历k 的for循环如果放在中间呢,同样是 j 与k 行程一个平面,i 是纵面,遍历的也是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) +![](https://file.kamacoder.com/pics/20240424115827.png) 同样不能完全用上初始化 和 上一层计算的结果。 @@ -283,7 +283,7 @@ for (int i = 1; i <= n; i++) { 图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240425112636.png) +![](https://file.kamacoder.com/pics/20240425112636.png) 求 节点1 到节点3 的最短距离,如果k循环放中间,程序的运行结果是 -1,也就是不能到达节点3。 diff --git a/problems/kamacoder/0098.所有可达路径.md b/problems/kamacoder/0098.所有可达路径.md index 7f56f9f4..2f0dcdcc 100644 --- a/problems/kamacoder/0098.所有可达路径.md +++ b/problems/kamacoder/0098.所有可达路径.md @@ -7,7 +7,7 @@ 【题目描述】 -给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个函数,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 +给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个程序,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 【输入描述】 @@ -43,7 +43,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240514103953.png) +![](https://file.kamacoder.com/pics/20240514103953.png) 用例解释: @@ -141,7 +141,7 @@ while (m--) { 我在 [图论理论基础篇](./图论理论基础.md) 举了一个例子: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: diff --git a/problems/kamacoder/0099.岛屿的数量广搜.md b/problems/kamacoder/0099.岛屿的数量广搜.md index d654e236..0da2f315 100644 --- a/problems/kamacoder/0099.岛屿的数量广搜.md +++ b/problems/kamacoder/0099.岛屿的数量广搜.md @@ -35,7 +35,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240516111613.png) +![](https://file.kamacoder.com/pics/20240516111613.png) 根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 @@ -50,7 +50,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -72,7 +72,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png) +![](https://file.kamacoder.com/pics/20250124094043.png) 超时写法 (从队列中取出节点再标记,注意代码注释的地方) diff --git a/problems/kamacoder/0099.岛屿的数量深搜.md b/problems/kamacoder/0099.岛屿的数量深搜.md index 5a21f387..06be9268 100644 --- a/problems/kamacoder/0099.岛屿的数量深搜.md +++ b/problems/kamacoder/0099.岛屿的数量深搜.md @@ -36,7 +36,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240516111613.png) +![](https://file.kamacoder.com/pics/20240516111613.png) 根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 @@ -50,7 +50,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 diff --git a/problems/kamacoder/0100.岛屿的最大面积.md b/problems/kamacoder/0100.岛屿的最大面积.md index 4f2f9d67..f2b9b901 100644 --- a/problems/kamacoder/0100.岛屿的最大面积.md +++ b/problems/kamacoder/0100.岛屿的最大面积.md @@ -33,7 +33,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517103410.png) +![](https://file.kamacoder.com/pics/20240517103410.png) 样例输入中,岛屿的最大面积为 4。 @@ -48,7 +48,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 @@ -63,7 +63,7 @@ 这里其实涉及到dfs的两种写法。 -写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 +写法一,dfs处理当前节点的相邻节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 ```CPP // 版本一 diff --git a/problems/kamacoder/0101.孤岛的总面积.md b/problems/kamacoder/0101.孤岛的总面积.md index 123e36ce..c8fe372c 100644 --- a/problems/kamacoder/0101.孤岛的总面积.md +++ b/problems/kamacoder/0101.孤岛的总面积.md @@ -37,7 +37,7 @@ 提示信息: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517105557.png) +![](https://file.kamacoder.com/pics/20240517105557.png) 在矩阵中心部分的岛屿,因为没有任何一个单元格接触到矩阵边缘,所以该岛屿属于孤岛,总面积为 1。 @@ -54,11 +54,11 @@ 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104632.png) +![](https://file.kamacoder.com/pics/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104651.png) +![](https://file.kamacoder.com/pics/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 @@ -72,10 +72,8 @@ #include using namespace std; int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向 -int count; // 统计符合题目要求的陆地空格数量 void dfs(vector>& grid, int x, int y) { grid[x][y] = 0; - count++; for (int i = 0; i < 4; i++) { // 向四个方向遍历 int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; @@ -109,16 +107,17 @@ int main() { if (grid[0][j] == 1) dfs(grid, 0, j); if (grid[n - 1][j] == 1) dfs(grid, n - 1, j); } - count = 0; + int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { - if (grid[i][j] == 1) dfs(grid, i, j); + if (grid[i][j] == 1) count++; } } cout << count << endl; } ``` + 采用广度优先搜索的代码如下: ```CPP @@ -126,13 +125,11 @@ int main() { #include #include using namespace std; -int count = 0; int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 void bfs(vector>& grid, int x, int y) { queue> que; que.push({x, y}); grid[x][y] = 0; // 只要加入队列,立刻标记 - count++; while(!que.empty()) { pair cur = que.front(); que.pop(); int curx = cur.first; @@ -143,7 +140,6 @@ void bfs(vector>& grid, int x, int y) { if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 if (grid[nextx][nexty] == 1) { que.push({nextx, nexty}); - count++; grid[nextx][nexty] = 0; // 只要加入队列立刻标记 } } @@ -169,15 +165,16 @@ int main() { if (grid[0][j] == 1) bfs(grid, 0, j); if (grid[n - 1][j] == 1) bfs(grid, n - 1, j); } - count = 0; + int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { - if (grid[i][j] == 1) bfs(grid, i, j); + if (grid[i][j] == 1) count++; } } cout << count << endl; } + ``` diff --git a/problems/kamacoder/0102.沉没孤岛.md b/problems/kamacoder/0102.沉没孤岛.md index a7e28df5..265ec31f 100644 --- a/problems/kamacoder/0102.沉没孤岛.md +++ b/problems/kamacoder/0102.沉没孤岛.md @@ -43,11 +43,11 @@ 提示信息: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517110932.png) +![](https://file.kamacoder.com/pics/20240517110932.png) 将孤岛沉没: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517110953.png) +![](https://file.kamacoder.com/pics/20240517110953.png) 数据范围: @@ -61,7 +61,7 @@ 思路依然是从地图周边出发,将周边空格相邻的陆地都做上标记,然后在遍历一遍地图,遇到 陆地 且没做过标记的,那么都是地图中间的 陆地 ,全部改成水域就行。 -有的录友可能想,我在定义一个 visited 二维数组,单独标记周边的陆地,然后遍历地图的时候同时对 数组board 和 数组visited 进行判断,决定 陆地是否变成水域。 +有的录友可能想,我再定义一个 visited 二维数组,单独标记周边的陆地,然后遍历地图的时候同时对 地图数组 和 数组visited 进行判断,决定 陆地是否变成水域。 这样做其实就有点麻烦了,不用额外定义空间了,标记周边的陆地,可以直接改陆地为其他特殊值作为标记。 @@ -73,7 +73,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517113813.png) +![](https://file.kamacoder.com/pics/20240517113813.png) 整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 diff --git a/problems/kamacoder/0103.水流问题.md b/problems/kamacoder/0103.水流问题.md index 3066c99f..5924cb18 100644 --- a/problems/kamacoder/0103.水流问题.md +++ b/problems/kamacoder/0103.水流问题.md @@ -48,7 +48,7 @@ 提示信息: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517115816.png) +![](https://file.kamacoder.com/pics/20240517115816.png) 图中的蓝色方块上的雨水既能流向第一组边界,也能流向第二组边界。所以最终答案为所有蓝色方块的坐标。 @@ -111,14 +111,14 @@ bool isResult(vector>& grid, int x, int y) { break; } } - // 第二边界右边 + // 第二边界下边 for (int j = 0; j < m; j++) { if (visited[n - 1][j]) { isSecond = true; break; } } - // 第二边界下边 + // 第二边界右边 for (int i = 0; i < n; i++) { if (visited[i][m - 1]) { isSecond = true; @@ -162,16 +162,17 @@ int main() { 同样从第二组边界的边上节点 逆流而上,将遍历过的节点也标记上。 -然后**两方都标记过的节点就是既可以流太平洋也可以流大西洋的节点**。 +然后**两方都标记过的节点就是既可以流向第一组边界也可以流向第二组边界的节点**。 -从第一组边界边上节点出发,如图: +从第一组边界边上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522120036.png) +![](https://file.kamacoder.com/pics/20250304174747.png) +从第二组边界上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -从第二组边界上节点出发,如图: +![](https://file.kamacoder.com/pics/20250304174801.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522120122.png) +最后,我们得到两个方向交界的这些节点,就是我们最后要求的节点。 按照这样的逻辑,就可以写出如下遍历代码:(详细注释) diff --git a/problems/kamacoder/0104.建造最大岛屿.md b/problems/kamacoder/0104.建造最大岛屿.md index ffb6cae3..483d7772 100644 --- a/problems/kamacoder/0104.建造最大岛屿.md +++ b/problems/kamacoder/0104.建造最大岛屿.md @@ -35,12 +35,12 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522154055.png) +![](https://file.kamacoder.com/pics/20240522154055.png) 对于上面的案例,有两个位置可将 0 变成 1,使得岛屿的面积最大,即 6。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522154110.png) +![](https://file.kamacoder.com/pics/20240522154110.png) 数据范围: @@ -70,11 +70,11 @@ 拿如下地图的岛屿情况来举例: (1为陆地) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829104834.png) +![](https://file.kamacoder.com/pics/20220829104834.png) -第一步,则遍历题目,并将岛屿到编号和面积上的统计,过程如图所示: +第一步,则遍历地图,并将岛屿的编号和面积都统计好,过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105644.png) +![](https://file.kamacoder.com/pics/20220829105644.png) 本过程代码如下: @@ -121,7 +121,7 @@ int largestIsland(vector>& grid) { 第二步过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105249.png) +![](https://file.kamacoder.com/pics/20220829105249.png) 也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 diff --git a/problems/kamacoder/0105.有向图的完全可达性.md b/problems/kamacoder/0105.有向图的完全可达性.md index 6901c655..cfe77c0d 100644 --- a/problems/kamacoder/0105.有向图的完全可达性.md +++ b/problems/kamacoder/0105.有向图的完全可达性.md @@ -33,7 +33,7 @@ 【提示信息】 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522174707.png) +![](https://file.kamacoder.com/pics/20240522174707.png) 从 1 号节点可以到达任意节点,输出 1。 @@ -48,7 +48,7 @@ 接下来我们再画一个图,从图里可以直观看出来,节点6 是 不能到达节点1 的 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522175451.png) +![](https://file.kamacoder.com/pics/20240522175451.png) 这就很容易让我们想起岛屿问题,只要发现独立的岛,就是不可到达的。 diff --git a/problems/kamacoder/0106.岛屿的周长.md b/problems/kamacoder/0106.岛屿的周长.md index 91a1a438..a1ef2a76 100644 --- a/problems/kamacoder/0106.岛屿的周长.md +++ b/problems/kamacoder/0106.岛屿的周长.md @@ -37,7 +37,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524115244.png) +![](https://file.kamacoder.com/pics/20240524115244.png) 岛屿的周长为 14。 @@ -57,14 +57,14 @@ 如果该陆地上下左右的空格是有水域,则说明是一条边,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524115933.png) +![](https://file.kamacoder.com/pics/20240524115933.png) 陆地的右边空格是水域,则说明找到一条边。 如果该陆地上下左右的空格出界了,则说明是一条边,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120105.png) +![](https://file.kamacoder.com/pics/20240524120105.png) 该陆地的下边空格出界了,则说明找到一条边。 @@ -114,7 +114,7 @@ int main() { 因为有一对相邻两个陆地,边的总数就要减2,如图红线部分,有两个陆地相邻,总边数就要减2 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120855.png) +![](https://file.kamacoder.com/pics/20240524120855.png) 那么只需要在计算出相邻岛屿的数量就可以了,相邻岛屿数量为cover。 diff --git a/problems/kamacoder/0107.寻找存在的路径.md b/problems/kamacoder/0107.寻找存在的路径.md index 779907c8..363a1884 100644 --- a/problems/kamacoder/0107.寻找存在的路径.md +++ b/problems/kamacoder/0107.寻找存在的路径.md @@ -40,7 +40,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527104432.png) +![](https://file.kamacoder.com/pics/20240527104432.png) 数据范围: diff --git a/problems/kamacoder/0108.冗余连接.md b/problems/kamacoder/0108.冗余连接.md index 121c2bfd..fe641f53 100644 --- a/problems/kamacoder/0108.冗余连接.md +++ b/problems/kamacoder/0108.冗余连接.md @@ -9,11 +9,11 @@ 有一个图,它是一棵树,他是拥有 n 个节点(节点编号1到n)和 n - 1 条边的连通无环无向图(其实就是一个线形图),如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240905163122.png) +![](https://file.kamacoder.com/pics/20240905163122.png) 现在在这棵树上的基础上,添加一条边(依然是n个节点,但有n条边),使这个图变成了有环图,如图 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240905164721.png) +![](https://file.kamacoder.com/pics/20240905164721.png) 先请你找出冗余边,删除后,使该图可以重新变成一棵树。 @@ -42,7 +42,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527110320.png) +![](https://file.kamacoder.com/pics/20240527110320.png) 图中的 1 2,2 3,1 3 等三条边在删除后都能使原图变为一棵合法的树。但是 1 3 由于是标准输入里最后出现的那条边,所以输出结果为 1 3 @@ -69,13 +69,13 @@ 如图所示,节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104720.png) +![](https://file.kamacoder.com/pics/20230604104720.png) 如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104330.png) +![](https://file.kamacoder.com/pics/20230604104330.png) 已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 @@ -157,7 +157,7 @@ int main() { 图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527110320.png) +![](https://file.kamacoder.com/pics/20240527110320.png) 输出示例 @@ -176,6 +176,81 @@ int main() { ### Java +```java +import java.util.Scanner; + +public class Main { + private static int[] father; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int pointNum = scanner.nextInt(); + father = new int[pointNum + 1]; + init(); + for (int i = 0; i < pointNum; i++) { + join(scanner.nextInt(), scanner.nextInt()); + } + } + + /** + * 并查集初始化 + */ + private static void init() { + for (int i = 1; i < father.length; i++) { + // 让每个元素指向自己 + father[i] = i; + } + } + + /** + * 并查集寻根 + * + * @param u + * @return + */ + private static int find(int u) { + // 判断 u 是否等于自己,如果是的话,直接返回自己 + // 如果不等于自己,就寻找根,寻找的时候,反复进行路径压缩 + return u == father[u] ? u : (father[u] = find(father[u])); + } + + /** + * 判断 u 和 v 是否同根 + * + * @param u + * @param v + * @return + */ + private static boolean isSame(int u, int v) { + return find(u) == find(v); + } + + /** + * 添加 边 到并查集,v 指向 u + * + * @param u + * @param v + */ + private static void join(int u, int v) { + // --if-- 如果两个点已经同根,说明他们的信息已经存储到并查集中了,直接返回即可 + // 寻找u的根 + int uRoot = find(u); + // 寻找v的根 + int vRoot = find(v); + if (uRoot == vRoot) { + // --if-- 如果u,v的根相同,说明两者已经连接了,直接输出 + System.out.println(u + " " + v); + return; + } + // --if-- 将信息添加到并查集 + father[vRoot] = uRoot; + } + +} +``` + + + ### Python ```python diff --git a/problems/kamacoder/0109.冗余连接II.md b/problems/kamacoder/0109.冗余连接II.md index 070bc685..78132a32 100644 --- a/problems/kamacoder/0109.冗余连接II.md +++ b/problems/kamacoder/0109.冗余连接II.md @@ -9,11 +9,11 @@ 有一种有向树,该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。有向树拥有 n 个节点和 n - 1 条边。如图:  - + 现在有一个有向图,有向图是在有向树中的两个没有直接链接的节点中间添加一条有向边。如图: - + 输入一个有向图,该图由一个有着 n 个节点(节点编号 从 1 到 n),n 条边,请返回一条可以删除的边,使得删除该条边之后该有向图可以被当作一颗有向树。 @@ -42,7 +42,7 @@ 提示信息 - + 在删除 2 3 后有向图可以变为一棵合法的有向树,所以输出 2 3 @@ -64,13 +64,13 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527115807.png) +![](https://file.kamacoder.com/pics/20240527115807.png) 找到了节点3 的入度为2,删 1 -> 3 或者 2 -> 3 。选择删顺序靠后便可。 但 入度为2 还有一种情况,情况二,只能删特定的一条边,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527151456.png) +![](https://file.kamacoder.com/pics/20240527151456.png) 节点3 的入度为 2,但在删除边的时候,只能删 这条边(节点1 -> 节点3),如果删这条边(节点4 -> 节点3),那么删后本图也不是有向树了(因为找不到根节点)。 @@ -81,7 +81,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527120531.png) +![](https://file.kamacoder.com/pics/20240527120531.png) 对于情况三,删掉构成环的边就可以了。 diff --git a/problems/kamacoder/0110.字符串接龙.md b/problems/kamacoder/0110.字符串接龙.md index 3b6c20ca..af343690 100644 --- a/problems/kamacoder/0110.字符串接龙.md +++ b/problems/kamacoder/0110.字符串接龙.md @@ -13,10 +13,14 @@ 2. 序列中最后一个字符串是 endStr。 -3. 每次转换只能改变一个字符。 +3. **每次转换只能改变一个位置的字符**(例如 ftr 可以转化 fty ,但 ftr 不能转化 frx)。 4. 转换过程中的中间字符串必须是字典 strList 中的字符串。 +5. beginStr 和 endStr 不在 字典 strList 中 + +6. 字符串中只有小写的26个字母 + 给你两个字符串 beginStr 和 endStr 和一个字典 strList,找到从 beginStr 到 endStr 的最短转换序列中的字符串数目。如果不存在这样的转换序列,返回 0。 输入描述 @@ -53,7 +57,7 @@ yhn 2 <= N <= 500

- +

@@ -61,22 +65,30 @@ yhn 以示例1为例,从这个图中可以看出 abc 到 def的路线 不止一条,但最短的一条路径上是4个节点。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240529121038.png) +![](https://file.kamacoder.com/pics/20250317105155.png) + 本题只需要求出最短路径的长度就可以了,不用找出具体路径。 所以这道题要解决两个问题: -* 图中的线是如何连在一起的 -* 起点和终点的最短路径长度 +1、图中的线是如何连在一起的 + +在搜索的过程中,我们可以枚举,用26个字母替换当前字符串的每一个字符,在看替换后 是否在 strList里出现过,就可以判断 两个字符串 是否是链接的。 + +2、起点和终点的最短路径长度 首先题目中并没有给出点与点之间的连线,而是要我们自己去连,条件是字符只能差一个。 所以判断点与点之间的关系,需要判断是不是差一个字符,**如果差一个字符,那就是有链接**。 -然后就是求起点和终点的最短路径长度,**这里无向图求最短路,广搜最为合适,广搜只要搜到了终点,那么一定是最短的路径**。因为广搜就是以起点中心向四周扩散的搜索。 +然后就是求起点和终点的最短路径长度,在无权图中,求最短路,用深搜或者广搜就行,没必要用最短路算法。 -**本题如果用深搜,会比较麻烦,要在到达终点的不同路径中选则一条最短路**。 而广搜只要达到终点,一定是最短路。 +**在无权图中,用广搜求最短路最为合适,广搜只要搜到了终点,那么一定是最短的路径**。因为广搜就是以起点中心向四周扩散的搜索。 + +**本题如果用深搜,会比较麻烦,要在到达终点的不同路径中选则一条最短路**。 + +而广搜只要达到终点,一定是最短路。 另外需要有一个注意点: diff --git a/problems/kamacoder/0117.软件构建.md b/problems/kamacoder/0117.软件构建.md index 58c17763..18802765 100644 --- a/problems/kamacoder/0117.软件构建.md +++ b/problems/kamacoder/0117.软件构建.md @@ -39,7 +39,7 @@ 文件依赖关系如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510192157.png) +![](https://file.kamacoder.com/pics/20240510192157.png) 所以,文件处理的顺序除了示例中的顺序,还存在 @@ -104,7 +104,7 @@ 以题目中示例为例如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510110836.png) +![](https://file.kamacoder.com/pics/20240510110836.png) 做拓扑排序的话,如果肉眼去找开头的节点,一定能找到 节点0 吧,都知道要从节点0 开始。 @@ -135,17 +135,17 @@ 1、找到入度为0 的节点,加入结果集 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113110.png) +![](https://file.kamacoder.com/pics/20240510113110.png) 2、将该节点从图中移除 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113142.png) +![](https://file.kamacoder.com/pics/20240510113142.png) ---------------- 1、找到入度为0 的节点,加入结果集 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113345.png) +![](https://file.kamacoder.com/pics/20240510113345.png) 这里大家会发现,节点1 和 节点2 入度都为0, 选哪个呢? @@ -153,19 +153,19 @@ 2、将该节点从图中移除 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113640.png) +![](https://file.kamacoder.com/pics/20240510113640.png) --------------- 1、找到入度为0 的节点,加入结果集 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113853.png) +![](https://file.kamacoder.com/pics/20240510113853.png) 节点2 和 节点3 入度都为0,选哪个都行,这里选节点2 2、将该节点从图中移除 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510114004.png) +![](https://file.kamacoder.com/pics/20240510114004.png) -------------- @@ -177,7 +177,7 @@ 如果有 有向环怎么办呢?例如这个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510115115.png) +![](https://file.kamacoder.com/pics/20240510115115.png) 这个图,我们只能将入度为0 的节点0 接入结果集。 @@ -252,13 +252,13 @@ while (que.size()) { 如果这里不理解,看上面的模拟过程第一步: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113110.png) +![](https://file.kamacoder.com/pics/20240510113110.png) 这事节点1 和 节点2 的入度为 1。 将节点0删除后,图为这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113142.png) +![](https://file.kamacoder.com/pics/20240510113142.png) 那么 节点0 作为出发点 所连接的节点的入度 就都做了 减一 的操作。 diff --git a/problems/kamacoder/0126.骑士的攻击astar.md b/problems/kamacoder/0126.骑士的攻击astar.md index 8ad32644..7d0096d5 100644 --- a/problems/kamacoder/0126.骑士的攻击astar.md +++ b/problems/kamacoder/0126.骑士的攻击astar.md @@ -9,7 +9,7 @@ 骑士移动规则如图,红色是起始位置,黄色是骑士可以走的地方。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240626104833.png) +![](https://file.kamacoder.com/pics/20240626104833.png) 棋盘大小 1000 x 1000(棋盘的 x 和 y 坐标均在 [1, 1000] 区间内,包含边界) @@ -108,7 +108,7 @@ int main() 我们来看一下广搜的搜索过程,如图,红色是起点,绿色是终点,黄色是要遍历的点,最后从 起点 找到 达到终点的最短路径是棕色。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611143712.png) +![](https://file.kamacoder.com/pics/20240611143712.png) 可以看出 广搜中,做了很多无用的遍历, 黄色的格子是广搜遍历到的点。 @@ -131,11 +131,11 @@ Astar 是一种 广搜的改良版。 有的是 Astar是 dijkstra 的改良版 在BFS中,我们想搜索,从起点到终点的最短路径,要一层一层去遍历。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611143712.png) +![](https://file.kamacoder.com/pics/20240611143712.png) 如果 使用A * 的话,其搜索过程是这样的,如图,图中着色的都是我们要遍历的点。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611195223.png) +![](https://file.kamacoder.com/pics/20240611195223.png) (上面两图中 最短路长度都是8,只是走的方式不同而已) diff --git a/problems/kamacoder/图论并查集理论基础.md b/problems/kamacoder/图论并查集理论基础.md index e463b956..5eb5127a 100644 --- a/problems/kamacoder/图论并查集理论基础.md +++ b/problems/kamacoder/图论并查集理论基础.md @@ -105,13 +105,13 @@ bool isSame(int u, int v) { 搜索过程像是一个多叉树中从叶子到根节点的过程,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602102619.png) +![](https://file.kamacoder.com/pics/20230602102619.png) 如果这棵多叉树高度很深的话,每次find函数 去寻找根的过程就要递归很多次。 我们的目的只需要知道这些节点在同一个根下就可以,所以对这棵多叉树的构造只需要这样就可以了,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602103040.png) +![](https://file.kamacoder.com/pics/20230602103040.png) 除了根节点其他所有节点都挂载根节点下,这样我们在寻根的时候就很快,只需要一步, @@ -226,7 +226,7 @@ join(3, 2); 此时构成的图是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230525111307.png) +![](https://file.kamacoder.com/pics/20230525111307.png) 此时问 1,3是否在同一个集合,我们调用 `join(1, 2); join(3, 2);` 很明显本意要表示 1,3是在同一个集合。 @@ -256,7 +256,7 @@ join(3, 2); 构成的图是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230525112101.png) +![](https://file.kamacoder.com/pics/20230525112101.png) 因为在join函数里,我们有find函数进行寻根的过程,这样就保证元素 1,2,3在这个有向图里是强连通的。 @@ -275,12 +275,12 @@ join(3, 2); 1、`join(1, 8);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122112727.png) +![](https://file.kamacoder.com/pics/20231122112727.png) 2、`join(3, 8);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122113857.png) +![](https://file.kamacoder.com/pics/20231122113857.png) 有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? @@ -288,12 +288,12 @@ join(3, 2); 3、`join(1, 7);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122114108.png) +![](https://file.kamacoder.com/pics/20231122114108.png) 4、`join(8, 5);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122114847.png) +![](https://file.kamacoder.com/pics/20231122114847.png) 这里8的根是3,那么 5 应该指向 8 的根 3,这里的原因,我们在上面「常见误区」已经讲过了。 但 为什么 图中 8 又直接指向了 3 了呢? @@ -310,11 +310,11 @@ int find(int u) { 5、`join(2, 9);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122115000.png) +![](https://file.kamacoder.com/pics/20231122115000.png) 6、`join(6, 9);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122115404.png) +![](https://file.kamacoder.com/pics/20231122115404.png) 这里为什么是 2 指向了 6,因为 9的根为 2,所以用2指向6。 @@ -347,13 +347,13 @@ rank表示树的高度,即树中结点层次的最大值。 例如两个集合(多叉树)需要合并,如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602172250.png) +![](https://file.kamacoder.com/pics/20230602172250.png) 树1 rank 为2,树2 rank 为 3。那么合并两个集合,是 树1 合入 树2,还是 树2 合入 树1呢? 我们来看两个不同方式合入的效果。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602172933.png) +![](https://file.kamacoder.com/pics/20230602172933.png) 这里可以看出,树2 合入 树1 会导致整棵树的高度变的更高,而 树1 合入 树2 整棵树的高度 和 树2 保持一致。 diff --git a/problems/kamacoder/图论广搜理论基础.md b/problems/kamacoder/图论广搜理论基础.md index d791d2c0..96e313fc 100644 --- a/problems/kamacoder/图论广搜理论基础.md +++ b/problems/kamacoder/图论广搜理论基础.md @@ -18,11 +18,11 @@ 我们用一个方格地图,假如每次搜索的方向为 上下左右(不包含斜上方),那么给出一个start起始位置,那么BFS就是从四个方向走出第一步。 -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220825104505.png) +![图一](https://file.kamacoder.com/pics/20220825104505.png) 如果加上一个end终止位置,那么使用BFS的搜索过程如图所示: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220825102653.png) +![图二](https://file.kamacoder.com/pics/20220825102653.png) 我们从图中可以看出,从start起点开始,是一圈一圈,向外搜索,方格编号1为第一步遍历的节点,方格编号2为第二步遍历的节点,第四步的时候我们找到终止点end。 @@ -30,7 +30,7 @@ 而且地图还可以有障碍,如图所示: -![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220825103900.png) +![图三](https://file.kamacoder.com/pics/20220825103900.png) 在第五步,第六步 我只把关键的节点染色了,其他方向周边没有去染色,大家只要关注关键地方染色的逻辑就可以。 diff --git a/problems/kamacoder/图论深搜理论基础.md b/problems/kamacoder/图论深搜理论基础.md index efe833a7..1611e00a 100644 --- a/problems/kamacoder/图论深搜理论基础.md +++ b/problems/kamacoder/图论深搜理论基础.md @@ -28,29 +28,29 @@ 如图一,是一个无向图,我们要搜索从节点1到节点6的所有路径。 -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707093643.png) +![图一](https://file.kamacoder.com/pics/20220707093643.png) 那么dfs搜索的第一条路径是这样的: (假设第一次延默认方向,就找到了节点6),图二 -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707093807.png) +![图二](https://file.kamacoder.com/pics/20220707093807.png) 此时我们找到了节点6,(遇到黄河了,是不是应该回头了),那么应该再去搜索其他方向了。 如图三: -![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707094011.png) +![图三](https://file.kamacoder.com/pics/20220707094011.png) 路径2撤销了,改变了方向,走路径3(红色线), 接着也找到终点6。 那么撤销路径2,改为路径3,在dfs中其实就是回溯的过程(这一点很重要,很多录友不理解dfs代码中回溯是用来干什么的) 又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图四中,路径4撤销(回溯的过程),改为路径5。 -![图四](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707094322.png) +![图四](https://file.kamacoder.com/pics/20220707094322.png) 又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图五,路径6撤销(回溯的过程),改为路径7,路径8 和 路径7,路径9, 结果发现死路一条,都走到了自己走过的节点。 -![图五](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707094813.png) +![图五](https://file.kamacoder.com/pics/20220707094813.png) 那么节点2所连接路径和节点3所链接的路径 都走过了,撤销路径只能向上回退,去选择撤销当初节点4的选择,也就是撤销路径5,改为路径10 。 如图图六: -![图六](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707095232.png) +![图六](https://file.kamacoder.com/pics/20220707095232.png) 上图演示中,其实我并没有把 所有的 从节点1 到节点6的dfs(深度优先搜索)的过程都画出来,那样太冗余了,但 已经把dfs 关键的地方都涉及到了,关键就两点: @@ -162,7 +162,7 @@ if (终止条件) { 终止添加不仅是结束本层递归,同时也是我们收获结果的时候。 -另外,其实很多dfs写法,没有写终止条件,其实终止条件写在了, 下面dfs递归的逻辑里了,也就是不符合条件,直接不会向下递归。这里如果大家不理解的话,没关系,后面会有具体题目来讲解。 +另外,其实很多dfs写法,没有写终止条件,其实终止条件写在了, 隐藏在下面dfs递归的逻辑里了,也就是不符合条件,直接不会向下递归。这里如果大家不理解的话,没关系,后面会有具体题目来讲解。 3. 处理目前搜索节点出发的路径 @@ -180,7 +180,7 @@ for (选择:本节点所连接的其他节点) { 如图七所示, 路径2 已经走到了 目的地节点6,那么 路径2 是如何撤销,然后改为 路径3呢? 其实这就是 回溯的过程,撤销路径2,走换下一个方向。 -![图七](https://code-thinking-1253855093.file.myqcloud.com/pics/20220708093544.png) +![图七](https://file.kamacoder.com/pics/20220708093544.png) ## 总结 diff --git a/problems/kamacoder/图论理论基础.md b/problems/kamacoder/图论理论基础.md index 50c36157..9bec3227 100644 --- a/problems/kamacoder/图论理论基础.md +++ b/problems/kamacoder/图论理论基础.md @@ -17,15 +17,15 @@ 有向图是指 图中边是有方向的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195737.png) +![](https://file.kamacoder.com/pics/20240510195737.png) 无向图是指 图中边没有方向: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195451.png) +![](https://file.kamacoder.com/pics/20240510195451.png) 加权有向图,就是图中边是有权值的,例如: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195821.png) +![](https://file.kamacoder.com/pics/20240510195821.png) 加权无向图也是同理。 @@ -35,7 +35,7 @@ 例如,该无向图中,节点4的度为5,节点6的度为3。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511115029.png) +![](https://file.kamacoder.com/pics/20240511115029.png) 在有向图中,每个节点有出度和入度。 @@ -45,7 +45,7 @@ 例如,该有向图中,节点3的入度为2,出度为1,节点1的入度为0,出度为2。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511115235.png) +![](https://file.kamacoder.com/pics/20240511115235.png) ## 连通性 @@ -56,11 +56,11 @@ 在无向图中,任何两个节点都是可以到达的,我们称之为连通图 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511102351.png) +![](https://file.kamacoder.com/pics/20240511102351.png) 如果有节点不能到达其他节点,则为非连通图,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511102449.png) +![](https://file.kamacoder.com/pics/20240511102449.png) 节点1 不能到达节点4。 @@ -72,7 +72,7 @@ 我们来看这个有向图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511104531.png) +![](https://file.kamacoder.com/pics/20240511104531.png) 这个图是强连通图吗? @@ -82,7 +82,7 @@ 下面这个有向图才是强连通图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511113101.png) +![](https://file.kamacoder.com/pics/20240511113101.png) ### 连通分量 @@ -91,7 +91,7 @@ 只看概念大家可能不理解,我来画个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511111559.png) +![](https://file.kamacoder.com/pics/20240511111559.png) 该无向图中 节点1、节点2、节点5 构成的子图就是 该无向图中的一个连通分量,该子图所有节点都是相互可达到的。 @@ -111,7 +111,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511112951.png) +![](https://file.kamacoder.com/pics/20240511112951.png) 节点1、节点2、节点3、节点4、节点5 构成的子图是强连通分量,因为这是强连通图,也是极大图。 @@ -128,6 +128,29 @@ 主要是 朴素存储、邻接表和邻接矩阵。 +关于朴素存储,这是我自创的名字,因为这种存储方式,就是将所有边存下来。 + +例如图: + +![](https://file.kamacoder.com/pics/20240511112951.png) + +图中有8条边,我们就定义 8 * 2的数组,即有n条边就申请n * 2,这么大的数组: + +![](https://file.kamacoder.com/pics/20250110114348.png) + +数组第一行:6 7,就表示节点6 指向 节点7,以此类推。 + +当然可以不用数组,用map,或者用 类 到可以表示出 这种边的关系。 + +这种表示方式的好处就是直观,把节点与节点之间关系很容易展现出来。 + +但如果我们想知道 节点1 和 节点6 是否相连,我们就需要把存储空间都枚举一遍才行。 + +这是明显的缺点,同时,我们在深搜和广搜的时候,都不会使用这种存储方式。 + +因为 搜索中,需要知道 节点与其他节点的链接情况,而这种朴素存储,都需要全部枚举才知道链接情况。 + +在图论章节的后面文章讲解中,我会举例说明的。大家先有个印象。 ### 邻接矩阵 @@ -139,7 +162,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) +![](https://file.kamacoder.com/pics/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间。 @@ -165,7 +188,7 @@ 邻接表的构造如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: diff --git a/problems/kamacoder/最短路问题总结篇.md b/problems/kamacoder/最短路问题总结篇.md index 54f91539..e83880da 100644 --- a/problems/kamacoder/最短路问题总结篇.md +++ b/problems/kamacoder/最短路问题总结篇.md @@ -17,7 +17,7 @@ 最短路算法比较复杂,而且各自有各自的应用场景,我来用一张表把讲过的最短路算法的使用场景都展现出来: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508121355.png) +![](https://file.kamacoder.com/pics/20240508121355.png) (因为A * 属于启发式搜索,和上面最短路算法并不是一类,不适合一起对比,所以没有放在一起) diff --git a/problems/qita/acm.md b/problems/qita/acm.md index 99928356..33dcd8a6 100644 --- a/problems/qita/acm.md +++ b/problems/qita/acm.md @@ -11,15 +11,15 @@ [知识星球](https://programmercarl.com/other/kstar.html)里很多录友的日常打卡中,都表示被 ACM模式折磨过: -
+
-
+
-
+
-
+
-
+
所以我正式推出:**卡码网**([https://kamacoder.com](https://kamacoder.com)),**专门帮助大家练习ACM模式**。 @@ -43,17 +43,17 @@ 来看看这极简的界面,没有烂七八糟的功能,只有刷题! -
+
在「状态」这里可以看到 大家提交的代码和判题记录,目前卡码网([https://kamacoder.com](https://kamacoder.com))几乎无时无刻都有卡友在提交代码。 看看大家周六晚上都在做什么,刷哪些题目。 -
+
提交代码的界面是这样的,**目前支持所有主流刷题语言**。 -
+
## 题解 @@ -63,7 +63,7 @@ [https://github.com/youngyangyang04/kamacoder-solutions](https://github.com/youngyangyang04/kamacoder-solutions) -
+
**欢迎去Github上star,欢迎fork,也欢迎来Github仓库贡献其他语言版本,成为contributor**。 @@ -71,7 +71,7 @@ 目前已经有两位录友贡献C和Java版本了。 -
+
期待在Github(https://github.com/youngyangyang04/kamacoder-solutions) 的contributors上也出现你的头像。 diff --git a/problems/qita/join.md b/problems/qita/join.md index c7e17588..027b8172 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -28,10 +28,10 @@ 点击这里Fetch upstream。 -
+
点击之后,这里就会显示最新的信息了 -
+
注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 @@ -39,18 +39,18 @@ 如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: -
+
点击 open pull request之后,就是如下画面,一个pull request有多个commit。 -
+
然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 -
+
此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 -
+
然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, @@ -78,27 +78,27 @@ C++代码 \`\`\` 例如这个commit,在添加java代码的时候,就直接添加代码 -
+
正确的格式应该是这样: -
+
一般发现问题,我也会在代码中给出评论: -
+
这样大家也可以学习一些 提交代码的规范方面的知识 有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: -
+
提交python代码的话,要注释好,是python2还是python3 例如这样: -
+
当然python2的话,只这么写就行 @@ -113,7 +113,7 @@ python代码 有的录友是一个pull request 里只有一个commit。 -
+
其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 @@ -127,13 +127,13 @@ python代码 例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 -
+
-
+
当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 -
+
### 说明具体是哪种方法 @@ -141,10 +141,10 @@ python代码 下面这位录友做的就很好 -
+
-
+
有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 @@ -162,7 +162,7 @@ python代码 有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 -
+
**代码规范从你我做起!** @@ -183,10 +183,10 @@ python代码 在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 -
+
大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 -
+
一天晚上分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] @@ -201,11 +201,11 @@ python代码 确保这种额外文件不要提交。 -
+
还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 -
+
前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 @@ -233,11 +233,11 @@ Go语言代码 甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 -
+
不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -
+
## 特别注意 diff --git a/problems/qita/kstar.md b/problems/qita/kstar.md deleted file mode 100644 index 7d1b045c..00000000 --- a/problems/qita/kstar.md +++ /dev/null @@ -1,131 +0,0 @@ -# 代码随想录知识星球 - -前一阵知识星球刚刚发布了[星球精华-大厂八股文(第三版)](https://programmercarl.com/other/kstar_baguwen.html) - -这份八股文,就有30w字,将近400张思维导图,表格,分析图,整个PDF将近900页的篇幅。 - -这些其实都是星球录友们,每日打卡的内容,但这我也仅仅是整理了一部分,因为信息量确实巨大。 - -目前星球里已经有将近1000 个精华帖: - -
- -同时还有[计算机2023届求职薪资PDF](https://programmercarl.com/other/2022salary.html)等一些列独家资料,都在星期置顶帖里: - -
- -星球里的录友都可以得到我1V1的指导,**我已经详细回答了7000+个问题**: (这个回答问题数量,可以看出我有劳模的潜质 ) - -
- -有的时候,大家还是需要过来人,给指点一点,甚至是“踹一脚” 就会想清楚很多。 - -
- -不仅我回答问题,我还会邀请星球里各个方向的录友来和大家一起交流具体技术问题,这个就是星球导师计划: (如果想提问的话,也在星球置顶1可以找到链接) - -
- -
- -可以看看星球导师计划里具体的问答: - -
- -同时我还给录友们至少修改了上千份的简历,我也总结了很多大家写简历上问题。在 「写简历」这个tab上,可以找到我总结的所有问题和简历模板 - -
- -【专业技能】【项目经验】【自我评价】都应该怎么写,面试时候 自我介绍,应该怎么说,我都给出了我的建议: - -
- - -如果你还在犹豫要不要加入的话,**可以进来体验三天,三天内点击知识星球APP右上角,可以自助全额退款**。 绝对不会坑大家! -
- - -一些录友当初也是进来 白嫖一波资料,就退款跑了,不过后面又加回来,例如这位录友: - -
- -**星球里的资料仅仅是辅助,更重要的是星球里的这一圈人,你会发现 这个圈子的质量非常高!** - -不仅仅是 **211、985录友非常之多**,关键是大家都非常努力上进! - -这是知识星球APP里可以看到,录友们的日常打卡: - -
- -刷星球上的内容,要刷朋友圈,刷抖音,有意义的多。 - -
- -
- -星球网页版是这样的: - -
- -加入星球,是很多录友当年做的最有意义的一件事情 - -
-
-
-
- -可以看看星球里的交流氛围: - -
- -
- -
- -
- -
- -大家的很多疑问在星球置顶3,我都做了详细的整理,录友都说我是“整理狂魔”,不过大家懒,我就得勤劳一些。 - -
- -星球置顶3的信息量非常大,不仅仅是整理各个求职方向的学习路线,还有大家的常见疑惑,我之前回答过的内容,都做了整理。 - -大家看完之后,其实对自己就会有明确的规划了。 - -
- -给大家看看星球置顶帖3的部分内容,以下仅仅是部分截图: - -
- -
- -
- -
- - -大家加入星球后,一定要看星球置顶帖和精华帖的内容,你会发现这里很有优秀录友的帖子,包括:各种资料,学习路线,学习心得,规划,职场发展等等。 - -
- -很多录友看完之后都更加明确了自己的方向。 - -
- - -相对于其他星球,「代码随想录」知识星球到底怎么样,可以看看录友们是怎么说的。 - -
- -最后也欢迎大家加入代码随想录[知识星球](https://mp.weixin.qq.com/s/wPaJumc8afuzWLo72yRlIw),**这里有很多优秀的人,有很多精彩的事!** - -
- -这里依然给出10元代金券,微信扫领代金券加入,如果感觉不值得,**三天内知识星球APP右上角直接全额退款!** 无任何套路。 - -
- - diff --git a/problems/qita/server.md b/problems/qita/server.md index 7e214d79..72476f57 100644 --- a/problems/qita/server.md +++ b/problems/qita/server.md @@ -51,7 +51,7 @@ 操作方式这样,我把命令包 包装成一个shell命令,想传那个文件,直接 uploadtomyserver,然后就返回可以下载的链接,这个文件也同时传到了我的服务器上。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211126165643.png) +![](https://file.kamacoder.com/pics/20211126165643.png) 我也把我的项目代码放在了github上: @@ -93,11 +93,11 @@ https://github.com/youngyangyang04/fileHttpServer 就是这样一个非常普通的查询页面。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211126160200.png) +![](https://file.kamacoder.com/pics/20211126160200.png) 查询通过之后,就会展现返现群二维码。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211127160558.png) +![](https://file.kamacoder.com/pics/20211127160558.png) 但要部署在服务器上,因为没有公网IP,别人用不了你的服务。 diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md index d5342c61..07d5540a 100644 --- a/problems/qita/shejimoshi.md +++ b/problems/qita/shejimoshi.md @@ -7,11 +7,11 @@ 所以卡码网 针对 23种设计,**推出了 23道编程题目,来帮助大家练习设计模式**。 -
+
这里的23到编程题目对应了 23种这几模式。 例如第一题,小明的购物车,就是单例模式: -
+
区别于网上其他教程,本教程的特点是: @@ -40,18 +40,18 @@ 同时还给全部整理到PDF上,这份PDF,我们写的很用心了,来个大家截个图: -
+
-
+
-
+
-
+
关于设计模式的题目,大家现在就可以去 卡码网(kamacoder)去做了。 关于这23道题目对应 设计模式精讲 PDF,也免费分享给录友们,大家可以加我的企业微信获取: -
+
已经有我企业微信的录友,直接发:设计模式,这四个字就好,我会直接发你。 diff --git a/problems/qita/tulunfabu.md b/problems/qita/tulunfabu.md index 28ee4638..b45b790d 100644 --- a/problems/qita/tulunfabu.md +++ b/problems/qita/tulunfabu.md @@ -8,7 +8,7 @@ 我知道录友们在等图论等太久了,其实我比大家都着急。 -![大家一直都在催](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105618.png) +![大家一直都在催](https://file.kamacoder.com/pics/20240613105618.png) 图论完整版目前已经开放在代码随想录网站:programmercarl.com @@ -20,7 +20,7 @@ * 拓扑排序 * 最短路算法 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104436.png) +![](https://file.kamacoder.com/pics/20240613104436.png) **耗时一年之久,代码随想录图论 终于面世了**! @@ -32,21 +32,21 @@ 随便截一些图,大家感受一下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104703.png) +![](https://file.kamacoder.com/pics/20240613104703.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104824.png) +![](https://file.kamacoder.com/pics/20240613104824.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104852.png) +![](https://file.kamacoder.com/pics/20240613104852.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104926.png) +![](https://file.kamacoder.com/pics/20240613104926.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105007.png) +![](https://file.kamacoder.com/pics/20240613105007.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105030.png) +![](https://file.kamacoder.com/pics/20240613105030.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105106.png) +![](https://file.kamacoder.com/pics/20240613105106.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105143.png) +![](https://file.kamacoder.com/pics/20240613105143.png) 具体内容,大家可以去代码随想录网站(programmercarl.com)去看看,非常精彩! @@ -203,19 +203,19 @@ cout << result[result.size() - 1]; 当大家通过 代码随想录 提升了编程与算法能力,考上研或者找到好工作的时候,于我来说已经是很幸福的事情: -![对笔试帮助大](https://code-thinking-1253855093.file.myqcloud.com/pics/20230914172536.png) +![对笔试帮助大](https://file.kamacoder.com/pics/20230914172536.png) -![华为od将近满分](https://code-thinking-1253855093.file.myqcloud.com/pics/20230914172607.png) +![华为od将近满分](https://file.kamacoder.com/pics/20230914172607.png) -![研究生复试](https://code-thinking-1253855093.file.myqcloud.com/pics/20240621103130.png) +![研究生复试](https://file.kamacoder.com/pics/20240621103130.png) -![红包感谢代码随想录366](https://code-thinking-1253855093.file.myqcloud.com/pics/20231123151310.png) +![红包感谢代码随想录366](https://file.kamacoder.com/pics/20231123151310.png) -![上岸亚马逊](https://code-thinking-1253855093.file.myqcloud.com/pics/20240206174151.png) +![上岸亚马逊](https://file.kamacoder.com/pics/20240206174151.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220718094112.png) +![](https://file.kamacoder.com/pics/20220718094112.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220718094332.png) +![](https://file.kamacoder.com/pics/20220718094332.png) 至此**图论内容 已完全免费开放在代码随想录网站(programmercarl.com),造福广大学习编程的录友们**! diff --git a/problems/qita/xunlianying.md b/problems/qita/xunlianying.md deleted file mode 100644 index f13290f3..00000000 --- a/problems/qita/xunlianying.md +++ /dev/null @@ -1,337 +0,0 @@ - -# 代码随想录算法训练营 - -::: tip 通知 -训练营35期,将于 4月3日开营,目前可以报名,报名后提前拉群,在群里等着开营就好。 -::: - -大家可以百度搜索:代码随想录算法训练营, 看看往期录友们在训练营里打卡总结的博客。 - -
- -这是训练营里录友坚持到最后一天的打卡,大家可以看看他们的博客是每天都有记录的: - -* [这种方式,有效逼我坚持下来(C++-小飞-嘉院大三)(精华)](https://blog.csdn.net/weixin_60353640/article/details/133797799) -* [完成比完美重要(Java-小姜-已工作/南京)(精华)](https://xie.infoq.cn/article/3d07b4040ceab0f546d66e3e1) -* [已经刷了500题的基础,参加训练营依然收获满满(Java-怪懒懒-求职)(精华)](https://blog.csdn.net/2301_78266314/article/details/132144046) - -* [算法超级弱,最后坚持下来了(Java-信任呢-上大研二)(精华)](https://blog.csdn.net/xinrenne/article/details/133267089) -* [第一次比较完整的刷题训练经历,群里氛围超级好(JAVA-雷贯三十三重天-北航研二)(精华)](https://blog.csdn.net/qq_44120129/article/details/133230372) -* [我全程坚持下来,还是很有成就感的(python-wj-待业)(精华)](https://blog.csdn.net/u013441272/article/details/133229421) - -* [一点基础都没有,坚持下来了(C++ 润 大二)(精华)](https://blog.csdn.net/m0_74583479/article/details/132776719) -* [这个钱花的很值得(C++-GMZ-研一)(精华)](https://blog.csdn.net/weixin_43303286/article/details/132796571) -* [看着名单里录友都在坚持,自己也要坚持(C++-凯-湖工大研三)(精华)](https://blog.csdn.net/weixin_62453859/article/details/132788830) - -* [一刷心得(Java-小何同学-广财大二)(精华)](https://juejin.cn/post/7272250890597531684) -* [花钱买服务、买环境、买时间(Java-古今-大工研二)(精华)](https://blog.csdn.net/dannky_Z/article/details/132532049) -* [一刷心得(java-唔哩凹-大三)(精华)](https://blog.csdn.net/iwtup/article/details/132545456) - -* [训练营结束有点不舍,坚持最久的一件事(C++-徐一-中科院研二)(精华)](https://blog.csdn.net/weixin_46108098/article/details/132158352) -* [同学推荐,报名训练营,坚持下来了(c++-刘浩-沈自所-研二)(精华)](https://blog.csdn.net/qq1156148707/article/details/132155446) - -* [每日的刷题训练真的艰难,但坚持下来了(C++-五-已工作福建)(精华)](https://blog.csdn.net/weixin_44952586/article/details/131909720) -* [加入训练营,就是因为这个气氛,只靠自己很难坚持(cpp-Lord HouSton-cqu研二)(精华)](https://blog.csdn.net/HSL13594379250/article/details/131889934) -* [很幸运,我坚持下来了,感觉收货满满(java-李-UCAS研0)(精华)](https://blog.csdn.net/ResNet156/article/details/131920163) -* [谈谈自己的收获,养成了写博客的习惯(java-翌-研二)(精华)](https://blog.csdn.net/weixin_47460244/article/details/131912316) - -* [养成了刷题的习惯(C++-热心市民C先生-南理工研一)(精华)](https://blog.csdn.net/qqq1521902442/article/details/131614999) -* [工作也坚持下来(Python-Hongying-已工作杭州)(精华)](https://blog.csdn.net/weixin_42286468/article/details/131628069) -* [入营不亏(C++-小叶子-云财研二)(精华)](https://blog.csdn.net/dream_aleaf/article/details/131613667) - -* [训练营一刷总结(Java-HQH-研二)](https://blog.csdn.net/weixin_43821876/article/details/128991822) -* [训练营总结,一群人才能走的更远(Java-Lixy-已工作南京)](https://blog.csdn.net/weixin_45368277/article/details/128997823) -* [训练营总结,中途🐑了,也坚持下来(C++-Jane-科大研二)](https://blog.csdn.net/Jane_10358/article/details/128977424) -* [这两个月有很多不可控因素,但依然坚持下来(java-hha-南工大二)](https://blog.csdn.net/qerwtrt4t/article/details/128975401) -* [训练营总结,最后坚持下来(C++ - 阿舟 - 已工作武汉)](https://blog.csdn.net/m0_74360161/article/details/129000723) - - -博客链接:[https://blog.csdn.net/m0_61724447/article/details/128443084](https://blog.csdn.net/m0_61724447/article/details/128443084) -
- -博客链接:[https://juejin.cn/post/7170304080504586254](https://juejin.cn/post/7170304080504586254) -
- -博客链接:[https://blog.csdn.net/weixin_44047621/article/details/128430623](https://blog.csdn.net/weixin_44047621/article/details/128430623) -
- -博客链接:[https://blog.csdn.net/weixin_47467016/article/details/128460565](https://blog.csdn.net/weixin_47467016/article/details/128460565v) -
- -也有一些录友,把总结发在训练营内部打卡表里,例如: - -昵称:java-低调-已工作 - -通过两个月的时间系统性的学习了算法,然后按照不同的题目去做分类,设计的刷题进度也很好,让自己有了一个质的提升,贵在坚持,好在自己也是坚持了下来,**通过自己的坚持,让自己养成了一个刷题的好习惯,这才是最难能可贵的**。 - -但是时间跨度有点大,还是要继续坚持之后自己去二刷,这样才能更好的巩固,把算法知识学习的更好。 - ---------- - -昵称:java-岂几岂几-毕业 - -收获真的很大,这是第一次刷算法题,清楚了面试高频题的题型,**巩固了之前摇摇欲坠的自学算法基础**。接下来计划是重刷随想录,并且补充上一亩三分地刷题区置顶贴里列出的题型,在巩固一刷的基础上增加做题量。 - ------------- - -昵称:python/go-ds-研三 - -跟着卡哥的训练营最大的收获就是把代码随想录都通读了一遍,因为进营之前就已经刷过不少力扣题了,但很多都是当时自己捣鼓出来或者看官方题解的。 - -而这一次的60天刷题,不管题目做没做过,都看过卡哥的代码随想录了,**这其中的区别也是最大的收获就是知识体系建立起来了**,越往后做题,条理越清晰。 - -即使有些题一刷还是做不太出来,但不再像之前自己做那样做题前后都是懵逼状态了,而是有一个清晰明了的判断了。 - -但coding能力还是有待改进,接下来要进行二刷,同时也祝卡哥的事业蒸蒸日上,代码随想录越办越好! - ------------- - -昵称:Python-ukn-研二 - -完美收官,有点小遗憾的是后面dp做得有点赶,没有沉下心来消化,接下来重点把自己不擅长的专题和重点专题二刷甚至三刷。 - -**跟着训练营练下来最大的感受是很有信心,有节奏有计划**,每过完一个专题,就多一分成就感,题感也越来越好,期待自己的规律二刷,谢谢一路坚持的小伙伴们!谢谢大佬助手和卡哥! - - ------------ - - -## 训练营的目的是什么? - -对于刷题,学算法,[《代码随想录》](https://programmercarl.com/other/publish.html)(programmercarl.com)已经把刷题顺序给大家列好了,大家跟着刷就行。 - -但即使这样,其实不少录友还会有很多疑问,不知道怎么用代码随想录,例如: - -* 卡哥,**有没有一起从0开始刷代码随想录的录友,想一起组个队** -* 卡哥您好,我是985准研一非科班,自学java, 然后现在在刷代码随想录,**请问需要每个题目的所有解法都掌握吗**?请教下卡哥正确的刷题姿势🙏 -* **我大概多久才能刷完代码随想录**? -* 二叉树,我只掌握 递归够用么? -* 很多解法,我是不是只用暴力就可以,**时间比较紧,我还要去掌握优化方法吗**? -* 卡哥,**请问跟着代码随想录刷题有答疑的服务吗**? 因为有的题目 自己写的怎么都不对,浪费很多时间,可能过来人指点一下立刻就知道。 -* 卡哥,我KMP太难了,我跳过可以吗? -* 卡哥,我进了刷题群,可是**大家刷题进度不尽相同,所以讨论起来经常不在一个频道上**。 -* 刚开始还看了一周代码随想录,后来又..摆烂了... **最近又重新再看代码随想录,然后卡住了又摆烂了好几天了**...... -* 卡哥,**我刷题很容易囫囵吞枣,虽然说代码随想录一刷,但很多内容根本没消化,在进度上欺骗自己**,好像一刷完了,但感觉自己理解的,不到30%。 -* 卡哥,**感觉之前刷的都忘了,能力没有什么提升,现在还是一道都不会做**。我一刷每道题都得先看看题解然后忘了再去看边看边写。 - -**以上这些是不是有戳中某些录友们的痛处**。 - -其实对于很多算法基础不太好的录友,即使资料已经很齐全,但还是需要一些规划和答疑。 - -而且在时间规划上,因为刚开始刷的录友,不知道 前方题目 是多大难度,所以 一开始计划 一天刷三道,往往因为遇到了一道难题,一天也解决不了,耽误了整体进度,甚至直接开始摆烂,下次再开始刷题可能就很久以后了...... - -所以 **代码随想录算法训练营** 帮助大家在规划时间内,有质量的完成代码随想录一刷。 - -我亲自给大家规划节奏,大家一起按照我的节奏来,规定时间内,一刷一定能把代码随想录所有内容吃透,然后大家自己去二刷,三刷就好了,师傅领进门修行在个人。 - -## 训练营提供一些什么呢? - -1.具体内容 - -针对代码随想录上,**195篇算法文章,主要题目150道**,手把手带大家刷完,帮大家做好详细刷题规划,每天布置刷题任务,监督博客记录总结。 - -任务布置 -
- -每日规划: -
- -训练营周期内,每天应该做哪些题目,同时我根据题目的难度,适当调整每天学习安排,不会是 每天固定3题的这种,而是根据难度而定。 - -我会告诉大家,哪些解法是一刷的时候必须掌握的,哪些解法可以二刷再去学习,哪些总结是必看的。 - -每日打卡: -
- -关于如果debug自己的代码,训练营会给具体建议: -
- -训练营群中每日讨论的重点内容都会做整理,在分享给大家训练营成员: -
- -**同时每天做针对大家的疑问做详细答疑,保证大家消化当天的学习内容**。 - -2.**气氛气氛还是气氛** - -训练营中,**大家都是同一个基础,同一个进度刷题,每天刷题题目都是一样的**,这样的一个学习群,大家讨论起来更有意义。 -**还有会监督机制**,训练营的成员要注册一个自己的博客(自己搭建或者使用博客网站都可以),每天要去写今日刷题心得和总结,来进行打卡。 - -
- -3.带大家写博客 - -很多录友平时刷题,或者学习技术,没有写博客的习惯,或者因为懒,就不写了。 - -但大家学了很多技术之后,发现 好像都忘了。。。 - -所以训练营会带着大家写博客,每天都要写博客,博客的标题,格式,我都帮大家规划好,倒逼自己养生记录的习惯。 - -因为训练营很多录友开始有了写博客的习惯,以下是一些录友博客的结尾部分: - -
- -
- -每天训练营群里会每天统计大家的博客情况。这样不仅可以监督自己总结,针对大家写的比较好的博客,会给予曝光,增加自己写博客的动力。 - -训练营里的录友们可以相互参考对方的博客,看谁总结的更好。 - -
- - -4.关于答疑 - -很多录友可能担心自己的问题,得不到解决,或者在群里和大家讨论,也没人回复 导致自己因为小问题卡了很久,甚至直接摆烂好一阵子。 - -所以训练营里大家的问题,我都会做答疑。 - -估计训练营里的问题会比较多,我也可能回答不过来。所以我会找了算法能力很强的助手协助我给大家答疑,也就是说,**大家刷题遇到问题,不会有后顾之忧,当天的问题,当天一定会得到解决**。 - -
- -当然训练营题目答疑,**仅限于 每天规划的题目**,并不会大家刷的其他算法题都做答疑,那样的话工作量很容易不可控(这里我也不会夸大承诺,欺骗大家报名之类的),如果是其他算法题可以在群里和大家交流。 - -### 训练营的资料是什么呢? - -**强调一下:训练营里所有的资料,都是我独立制作而且是开源免费的:即代码随想录网站(programmercarl.com),Github:https://github.com/youngyangyang04/leetcode-master和[代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g)** - -训练营提供给大家的服务是**规划,监督,指导和答疑**。 - -至于代码随想录算法内容的质量如何,这个已经是有口皆碑了,基本是面试求职必刷的资料。 - -
- -
- -而且代码随想录开源的内容要比市面上 大家付费几百,上千元报的算法训练营的资料都要好的多。 - -毕竟内容是开源的,质量如何 大家自己去看就好。 - -## 训练营的学习方式 - -组织方式:一个学习微信群(180人左右),大家进群之后,等群公告就好,我会通知开始时间和每日刷题计划。 - -所需时间:训练营为期60天(两个月),群里每天会布置学习任务,只要大家跟上节奏,60天一定可以刷完代码随想录。 - -每日任务:需要花费3-4个小时左右的时间来完成。这是针对一般算法水平的学习速度来规划的时间,不同水平会有差异。 - -每周周日会休息一天,没跟上进度的录友,可以跟进度,跟上进度的录友可以复习或者适当放松一下。 - -监督机制:训练营里,每天会针对大家每天所刷的题目做答疑,同时也会有监督打卡机制,在群公告里会详细描述。 - -所需语言:**所有语种都可以**,毕竟代码随想录几乎支持所有主流语言,**也会针对大家所用的语言做针对性答疑**。 - -## 开营时间 - -**训练营开始常态化报名,即一直可以报名,当人满180人的时候,就开始新的一期**。 最新的一期可以看文章评论区,或者文章开头。 - -## 训练营的价格 - -大家应该最关心的是价格了,**定价依然是268元**,注意这是两个月训练营的费用,而且是全程规划,指导,监督和答疑。 - -(对于[知识星球](https://programmercarl.com/other/kstar.html)里的录友的话,训练营会立减30元,也就是238元,后面如果推出其他服务,星球录友都相当于VIP,都会有优惠。当然如果你已经报了训练营,再去报知识星球,并不给再给大家优惠了,一定要先是星球成员,再报训练营才有优惠) - -大家能在市面上找到算法训练营都价格不菲,基本都是上千的单价,**而且内容和质量并没有 代码随想录 优质**。 - -后面一定会涨价的,**如果你确实需要有人带,有监督,给规划,有答疑,能花两个月时间更下来的话,还是早报早学习**。 - -## 我适合报名吗? - -符合一下特点的录友可以报名: - -* 基础比较差,没刷过代码随想录或者刚开始刷 -* 刷过一些代码随想录的题目了,感觉掌握不扎实,想用2个月时间系统重刷一遍 -* 自己刷题,**很容易遇到各种代码问题,需要有人答疑** -* 以前一刷过,但基本都忘了,想高质量二刷 -* **想找队友,一起从0刷代码随想录** -* 自控能力差,遇到点问题就容易躺好一阵子,需要别人监督学习 -* **想有一个规划时间,来刷完代码随想录** -* 不知道代码随想录中哪些解法是必备的,哪些解法是可以简单了解的 -* 刷题总会忘,感觉刷了和没刷差不多,**不擅长做总结,不擅长写博客记录心得**,自己也懒得写博客写总结 - -以下录友不合适报名: - -* 自学能力强,代码随想录资料都是开源的,刷题顺序也列好了,自学能力强的录友自己学就行 -* 有算法和代码基础,基本算法题遇到的问题,都能通过自己debug解决 -* 没有两个月时间,每天也不能抽出那么多时间学习算法 -* 算法0基础,基本的数据结构都没听说过,例如数组,链表。 -* 编程0基础,基本的编程语言还不会,因为训练营还是默认大家会熟悉所用编程语言里的各种容器的使用 - -**训练营不限编程语言**,任何语言都可以报名,都会答疑。 - - -## 常见疑问 - -**海外录友有时差可以报名吗**? - -可以的,一期就有很多海外的录友,有疑问在群里也会回复,而且群里讨论的重点内容,都会有总结,不用担心错过了精彩内容。 - -**已经工作的录友适合报名吗**? - -适合报名。对于工作的录友,每天未必说一定挤出3-4个小时来刷题。 - -对于时间充足的录友,要刷拓展题,要写博客作总结。 如果时间紧张,任务上是可以适当精简。 - -所以每日任务弹性还是比较大的,至少跟上进度保证每天的题目代码提交通过了,看看群里的讨论,自己理解加深了就可以。 - -工作的录友要学会挤时间,训练营一期录友有不少是工作的,他们是这么搞的: - -训练营每日晚上提前发布明天的任务, 他们第二天通勤 时候 可以先看题想思路,白天抽空看文章解析 看看思路是否一致,看看群里讨论内容,晚上下班可以一口气把当天的题目刷完。 - -加入训练营,每日对自己有一个压迫感,挤一挤 时间就有了。 - -对于工作的录友,我之前本来计划是安排一期 工作日题量小 休息日题量多一些的训练营,但通过一期发现,包括已经工作的录友,**大家休息日真的没有心思学习,甚至“比工作日更忙”**,所以理想很丰满,现实很骨感。 - -**要不要搞三个月四个月半年的训练营**? - -目前来看四个月以上的时间有点太长了,时间长价格也会高,毕竟要服务的时间长了。 - -而且刷题在于一鼓作气,把时间拉的太长,很多录友都是前期 动力十足,后面无论是 如何@ 如何公告 如果催大家 赶进度,大家都会无动于衷,从最终效果来看 战线不能太长。 - -所以没有逼自己一把 跟上进度的决心,就算搞一年时间的训练营,该放弃的还是会放弃。 - -至于三个月的训练营,是可以考虑的,不过安排时间还要待定。 - -## 报名方式 - -* 正常支付,价格:268 (支付成功后,支付记录发给客服 -* 知识星球录友支付,价格:238 (支付成功后,[代码随想录知识星球](https://programmercarl.com/other/kstar.html)截图 和 支付记录发给客服 -* 往期算法训练营录友再次报名,价格:130 (支付成功后,往期训练营群或者支付记录 和 本次支付记录发给客服 - -(**注意一定要是[代码随想录知识星球](https://programmercarl.com/other/kstar.html)成员才会有优惠**) - -支付宝支付如下: - -
- -[微信支付点击这里](https://www.programmercarl.com/other/weixinzhifu.html) - -付款后,将付款截图发给客服,客服会在24h内统一回复,**所以大家发给客服信息不要急,当天一定会回复的**。 - -
- -关于训练营的任何问题,可以在客服这里咨询! - - - -## 最后 - -训练营其实算是代码随想录的一个补充,其内容都是免费开放的,有学习能力的录友自己学习就好。 - -单就从我的 [代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g) 来说,质量如何,大家可以去看评论区,我完全可以把它做成付费的视频课,但我还是选择免费开放给大家,目前一周会更新四个算法视频,已经快把二叉树系列更完了。 - -之所以做训练营,是因为大家太多的问题,不是视频或者文章教程可以解决的,需要的是规划,组织,监督和答疑。 - -所以我才组织训练营,搞成付费的也是为了质量更高一些,同时也是因为需要一些门槛,要不然就和普通刷题群没什么区别了。 - -等大家跟着代码随想录训练营一路走下来之后,大家再回顾自己两个月学习的内容和总结的博客,**一定会发现 这个价格 物超所值**! - -关于训练营的任何疑问都可以扫码联系客服 - -
- - - - - - diff --git a/problems/toolgithub.sh b/problems/toolgithub.sh index 9e005f0c..4a0b0a75 100644 --- a/problems/toolgithub.sh +++ b/problems/toolgithub.sh @@ -31,11 +31,17 @@ do # 添加开头 # 记得从后向前添加 - ex -sc '1i|

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

' -cx $i - ex -sc '1i|' -cx $i - ex -sc '1i| ' -cx $i - ex -sc '1i|' -cx $i - ex -sc '1i|

' -cx $i + # ex -sc '1i|

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

' -cx $i + # ex -sc '1i|' -cx $i + # ex -sc '1i| ' -cx $i + # ex -sc '1i|' -cx $i + # ex -sc '1i|

' -cx $i + + ex -sc '1i|* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)' -cx $i + ex -sc '1i|* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)' -cx $i + ex -sc '1i|* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)' -cx $i + + # echo '## 其他语言版本' >> $i # echo '\n' >> $i # echo 'Java:' >> $i @@ -48,10 +54,10 @@ do # 添加结尾 - echo '

' >> $i - echo '' >> $i - echo ' ' >> $i - echo '' >> $i + # echo '

' >> $i + # echo '' >> $i + # echo ' ' >> $i + # echo '' >> $i # echo '-----------------------' >> $i diff --git a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md index c2f5efd9..69d6aa9c 100644 --- a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md +++ b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划之编辑距离总结篇 @@ -196,7 +194,3 @@ class Solution { -

- - - diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 42d78ae3..7fa2b6ec 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 二叉树:以为使用了递归,其实还隐藏着回溯 @@ -687,7 +685,3 @@ impl Solution { ``` -

- - - diff --git a/problems/二叉树总结篇.md b/problems/二叉树总结篇.md index 8db40d65..a67cf5db 100644 --- a/problems/二叉树总结篇.md +++ b/problems/二叉树总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 二叉树:总结篇!(需要掌握的二叉树技能都在这里了) @@ -92,10 +90,9 @@ * 递归:中序,双指针操作 * 迭代:模拟中序,逻辑相同 * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) - + * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 - * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) - +* [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) * 递归:中序,双指针操作累加 * 迭代:模拟中序,逻辑相同 @@ -150,7 +147,7 @@ 二叉树专题汇聚为一张图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030125421.png) +![](https://file.kamacoder.com/pics/20211030125421.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 @@ -159,7 +156,4 @@ -

- - - + diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index c6658277..1fbbc9d9 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -17,7 +15,7 @@ 题目分类大纲如下: -二叉树大纲 +二叉树大纲 说到二叉树,大家对于二叉树其实都很熟悉了,本文呢我也不想教科书式的把二叉树的基础内容再啰嗦一遍,所以以下我讲的都是一些比较重点的内容。 @@ -33,7 +31,7 @@ 如图所示: - + 这棵二叉树为满二叉树,也可以说深度为k,有2^k-1个节点的二叉树。 @@ -48,7 +46,7 @@ 我来举一个典型的例子如题: - + 相信不少同学最后一个二叉树是不是完全二叉树都中招了。 @@ -65,7 +63,7 @@ 下面这两棵树都是搜索树 - + ### 平衡二叉搜索树 @@ -74,7 +72,7 @@ 如图: - + 最后一棵 不是平衡二叉树,因为它的左右两个子树的高度差的绝对值超过了1。 @@ -93,13 +91,13 @@ 链式存储如图: - + 链式存储是大家很熟悉的一种方式,那么我们来看看如何顺序存储呢? 其实就是用数组来存储二叉树,顺序存储的方式如图: - + 用数组来存储二叉树如何遍历的呢? @@ -146,7 +144,7 @@ 大家可以对着如下图,看看自己理解的前后中序有没有问题。 - + 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 @@ -314,7 +312,3 @@ public class TreeNode } ``` -

- - - diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index a6d4e3ff..d001e0f7 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 统一写法是一种什么感觉 @@ -970,8 +968,4 @@ public IList PostorderTraversal(TreeNode root) ``` -

- - - diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index a3c5b38f..e011612c 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 听说还可以用非递归的方式 @@ -119,7 +117,7 @@ public: 再来看后序遍历,先序遍历是中左右,后序遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: -![前序到后序](https://code-thinking-1253855093.file.myqcloud.com/pics/20200808200338924.png) +![前序到后序](https://file.kamacoder.com/pics/20200808200338924.png) **所以后序遍历只需要前序遍历的代码稍作修改就可以了,代码如下:** @@ -797,7 +795,3 @@ public IList PostorderTraversal(TreeNode root) } ``` -

- - - diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 8f61b8c6..ffa3ff6c 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 一看就会,一写就废! @@ -728,7 +726,3 @@ function traversal($root, array &$output) { ``` -

- - - diff --git a/problems/前序/ACM模式.md b/problems/前序/ACM模式.md index 313264fb..f1ff3a5f 100644 --- a/problems/前序/ACM模式.md +++ b/problems/前序/ACM模式.md @@ -5,15 +5,15 @@ 平时大家在力扣上刷题,就是 核心代码模式,即给你一个函数,直接写函数实现,例如这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193631.png) +![](https://file.kamacoder.com/pics/20231109193631.png) 而ACM模式,是程序头文件,main函数,数据的输入输出都要自己处理,例如这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193743.png) +![](https://file.kamacoder.com/pics/20231109193743.png) 大家可以发现 右边代码框什么都没有,程序从头到尾都需要自己实现,本题如果写完代码是这样的: (细心的录友可以发现和力扣上刷题是不一样的) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193931.png) +![](https://file.kamacoder.com/pics/20231109193931.png) **如果大家从一开始学习算法就一直在力扣上的话,突然切到ACM模式会非常不适应**。 @@ -21,15 +21,15 @@ 知识星球里也有很多录友,因为不熟悉ACM模式在面试的过程中吃了不少亏。 -

+
-
+
-
+
-
+
-
+
## 面试究竟怎么考? @@ -53,7 +53,7 @@ 你只要能把卡码网首页的25道题目 都刷了 ,就把所有的ACM输入输出方式都练习到位了,不会有任何盲区。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109195056.png) +![](https://file.kamacoder.com/pics/20231109195056.png) 而且你不用担心,题目难度太大,直接给自己劝退,**卡码网的前25道题目都是我精心制作的,难度也是循序渐进的**,大家去刷一下就知道了。 diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index 48781eda..086e6e0e 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.md @@ -15,7 +15,7 @@ 其输入用例,就是用一个数组来表述 二叉树,如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914222335.png) +![](https://file.kamacoder.com/pics/20210914222335.png) 一直跟着公众号学算法的录友 应该知道,我在[二叉树:构造二叉树登场!](https://mp.weixin.qq.com/s/Dza-fqjTyGrsRw4PWNKdxA),已经讲过,**只有 中序与后序 和 中序和前序 可以确定一棵唯一的二叉树。 前序和后序是不能确定唯一的二叉树的**。 @@ -24,7 +24,7 @@ 很明显,是后台直接明确了构造规则。 再看一下 这个 输入序列 和 对应的二叉树。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914222335.png) +![](https://file.kamacoder.com/pics/20210914222335.png) 从二叉树 推导到 序列,大家可以发现这就是层序遍历。 @@ -36,7 +36,7 @@ 顺序存储,就是用一个数组来存二叉树,其方式如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914223147.png) +![](https://file.kamacoder.com/pics/20210914223147.png) 那么此时大家是不是应该知道了,数组如何转化成 二叉树了。**如果父节点的数组下标是i,那么它的左孩子下标就是i * 2 + 1,右孩子下标就是 i * 2 + 2**。 @@ -80,7 +80,7 @@ TreeNode* construct_binary_tree(const vector& vec) { 这个函数最后返回的 指针就是 根节点的指针, 这就是 传入二叉树的格式了,也就是 力扣上的用例输入格式,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914224422.png) +![](https://file.kamacoder.com/pics/20210914224422.png) 也有不少同学在做ACM模式的题目,就经常疑惑: @@ -176,7 +176,7 @@ int main() { 和 [538.把二叉搜索树转换为累加树](https://mp.weixin.qq.com/s/rlJUFGCnXsIMX0Lg-fRpIw) 中的输入是一样的 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914222335.png) +![](https://file.kamacoder.com/pics/20210914222335.png) 这里可能又有同学疑惑,你这不一样啊,题目是null,你为啥用-1。 @@ -184,11 +184,11 @@ int main() { 在来看,测试代码输出的效果: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914230045.png) +![](https://file.kamacoder.com/pics/20210914230045.png) 可以看出和 题目中输入用例 这个图 是一样一样的。 只不过题目中图没有把 空节点 画出来而已。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914230118.png) +![](https://file.kamacoder.com/pics/20210914230118.png) 大家可以拿我的代码去测试一下,跑一跑。 @@ -205,7 +205,7 @@ int main() { **[知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)**里有的录友已经开始三刷: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727234031.png) +![](https://file.kamacoder.com/pics/20210727234031.png) 只做过一遍,真的就是懂了一点皮毛, 第二遍刷才有真的对各个题目有较为深入的理解,也会明白 我为什么要这样安排刷题的顺序了。 diff --git a/problems/前序/vim.md b/problems/前序/vim.md index 58101999..13935b91 100644 --- a/problems/前序/vim.md +++ b/problems/前序/vim.md @@ -93,7 +93,7 @@ sh install.sh 当然 还有很多,我还详细写了PowerVim的快捷键,使用方法,插件,配置,等等,都在Github主页的README上。当时我的Github上写的都是英文README,这次为了方便大家阅读,我又翻译成中文README。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211013102249.png) +![](https://file.kamacoder.com/pics/20211013102249.png) Github地址:[https://github.com/youngyangyang04/PowerVim](https://github.com/youngyangyang04/PowerVim) diff --git a/problems/前序/代码风格.md b/problems/前序/代码风格.md index 95f4f129..33891632 100644 --- a/problems/前序/代码风格.md +++ b/problems/前序/代码风格.md @@ -57,7 +57,7 @@ 我做了一下总结如图: -![编程风格](https://code-thinking-1253855093.file.myqcloud.com/pics/20201119173039835.png) +![编程风格](https://file.kamacoder.com/pics/20201119173039835.png) ### 水平留白(代码空格) diff --git a/problems/前序/内存消耗.md b/problems/前序/内存消耗.md index f70564e4..88fb129e 100644 --- a/problems/前序/内存消耗.md +++ b/problems/前序/内存消耗.md @@ -19,7 +19,7 @@ 如果我们写C++的程序,就要知道栈和堆的概念,程序运行时所需的内存空间分为 固定部分,和可变部分,如下: -![C++内存空间](https://code-thinking-1253855093.file.myqcloud.com/pics/20210309165950660.png) +![C++内存空间](https://file.kamacoder.com/pics/20210309165950660.png) 固定部分的内存消耗 是不会随着代码运行产生变化的, 可变部分则是会产生变化的 @@ -41,7 +41,7 @@ 想要算出自己程序会占用多少内存就一定要了解自己定义的数据类型的大小,如下: -![C++数据类型的大小](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193045440.png) +![C++数据类型的大小](https://file.kamacoder.com/pics/20200804193045440.png) 注意图中有两个不一样的地方,为什么64位的指针就占用了8个字节,而32位的指针占用4个字节呢? @@ -109,7 +109,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第一种就是内存对齐的情况,如图: -![内存对齐](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193307347.png) +![内存对齐](https://file.kamacoder.com/pics/20200804193307347.png) 一字节的char占用了四个字节,空了三个字节的内存地址,int数据从地址4开始。 @@ -117,7 +117,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第二种是没有内存对齐的情况如图: -![非内存对齐](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193353926.png) +![非内存对齐](https://file.kamacoder.com/pics/20200804193353926.png) char型的数据和int型的数据挨在一起,该int数据从地址1开始,那么CPU想要读这个数据的话来看看需要几步操作: diff --git a/problems/前序/时间复杂度.md b/problems/前序/时间复杂度.md index 61f0a7ef..045646ff 100644 --- a/problems/前序/时间复杂度.md +++ b/problems/前序/时间复杂度.md @@ -38,7 +38,7 @@ 同样的同理再看一下快速排序,都知道快速排序是O(nlogn),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。 **但是我们依然说快速排序是O(nlogn)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示: -![时间复杂度4,一般情况下的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728185745611-20230310123844306.png) +![时间复杂度4,一般情况下的时间复杂度](https://file.kamacoder.com/pics/20200728185745611-20230310123844306.png) 我们主要关心的还是一般情况下的数据形式。 @@ -49,7 +49,7 @@ 如下图中可以看出不同算法的时间复杂度在不同数据输入规模下的差异。 -![时间复杂度,不同数据规模的差异](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447384-20230310124015324.png) +![时间复杂度,不同数据规模的差异](https://file.kamacoder.com/pics/20200728191447384-20230310124015324.png) 在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。 @@ -115,7 +115,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 为什么可以这么做呢?如下图所示: -![时间复杂度1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447349-20230310124032001.png) +![时间复杂度1.png](https://file.kamacoder.com/pics/20200728191447349-20230310124032001.png) 假如有两个算法的时间复杂度,分别是log以2为底n的对数和log以10为底n的对数,那么这里如果还记得高中数学的话,应该不难理解`以2为底n的对数 = 以2为底10的对数 * 以10为底n的对数`。 diff --git a/problems/前序/程序员简历.md b/problems/前序/程序员简历.md index 7fbfa1fd..1bdcccd5 100644 --- a/problems/前序/程序员简历.md +++ b/problems/前序/程序员简历.md @@ -103,13 +103,13 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 最后福利,把我的简历模板贡献出来!如下图所示。 -![简历模板](https://code-thinking-1253855093.file.myqcloud.com/pics/20200803175538158.png) +![简历模板](https://file.kamacoder.com/pics/20200803175538158.png) 这里是简历模板中Markdown的代码:[https://github.com/youngyangyang04/Markdown-Resume-Template](https://github.com/youngyangyang04/Markdown-Resume-Template) ,可以fork到自己Github仓库上,按照这个模板来修改自己的简历。 **Word版本的简历,添加如下企业微信,通过之后就会发你word版本**。 -
+
如果已经有我的企业微信,直接回复:简历模板,就可以了。 diff --git a/problems/前序/算法超时.md b/problems/前序/算法超时.md index 34ebe6de..ca1a422c 100644 --- a/problems/前序/算法超时.md +++ b/problems/前序/算法超时.md @@ -8,7 +8,7 @@ ## 超时是怎么回事 -![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117-20230310124308704.png) +![程序超时](https://file.kamacoder.com/pics/20200729112716117-20230310124308704.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -124,11 +124,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460-20230310124315093.png) +![程序超时2](https://file.kamacoder.com/pics/20200729200018460-20230310124315093.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下 $O(n^2)$ 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970-20230310124318532.png) +![程序超时3](https://file.kamacoder.com/pics/2020072919590970-20230310124318532.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -136,7 +136,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 $O(n)$ 少一个数量级,因为 $\log n$ 的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407-20230310124322232.png) +![程序超时4](https://file.kamacoder.com/pics/20200729195729407-20230310124322232.png) $O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -144,7 +144,7 @@ $O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算, **整体测试数据整理如下:** -![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175-20230310124325152.png) +![程序超时1](https://file.kamacoder.com/pics/20201208231559175-20230310124325152.png) 至于 $O(\log n)$ 和 $O(n^3)$ 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 diff --git a/problems/前序/递归算法的时间与空间复杂度分析.md b/problems/前序/递归算法的时间与空间复杂度分析.md index 39513a91..035399ce 100644 --- a/problems/前序/递归算法的时间与空间复杂度分析.md +++ b/problems/前序/递归算法的时间与空间复杂度分析.md @@ -29,7 +29,7 @@ int fibonacci(int i) { 可以看出上面的代码每次递归都是O(1)的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图: -![递归空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305093200104.png) +![递归空间复杂度分析](https://file.kamacoder.com/pics/20210305093200104.png) 从图中,可以看出f(5)是由f(4)和f(3)相加而来,那么f(4)是由f(3)和f(2)相加而来 以此类推。 @@ -196,7 +196,7 @@ int main() 在看递归的深度是多少呢?如图所示: -![递归空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305094749554.png) +![递归空间复杂度分析](https://file.kamacoder.com/pics/20210305094749554.png) 递归第n个斐波那契数的话,递归调用栈的深度就是n。 @@ -214,7 +214,7 @@ int fibonacci(int i) { 最后对各种求斐波那契数列方法的性能做一下分析,如题: -![递归的空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305095227356.png) +![递归的空间复杂度分析](https://file.kamacoder.com/pics/20210305095227356.png) 可以看出,求斐波那契数的时候,使用递归算法并不一定是在性能上是最优的,但递归确实简化的代码层面的复杂度。 diff --git a/problems/前序/递归算法的时间复杂度.md b/problems/前序/递归算法的时间复杂度.md index 1ea1e65b..befe5498 100644 --- a/problems/前序/递归算法的时间复杂度.md +++ b/problems/前序/递归算法的时间复杂度.md @@ -69,7 +69,7 @@ int function3(int x, int n) { 我们来分析一下,首先看递归了多少次呢,可以把递归抽象出一棵满二叉树。刚刚同学写的这个算法,可以用一棵满二叉树来表示(为了方便表示,选择n为偶数16),如图: -![递归算法的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209193909426.png) +![递归算法的时间复杂度](https://file.kamacoder.com/pics/20201209193909426.png) 当前这棵二叉树就是求x的n次方,n为16的情况,n为16的时候,进行了多少次乘法运算呢? @@ -79,7 +79,7 @@ int function3(int x, int n) { 这么如果是求x的n次方,这个递归树有多少个节点呢,如下图所示:(m为深度,从0开始) -![递归求时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728195531892.png) +![递归求时间复杂度](https://file.kamacoder.com/pics/20200728195531892.png) **时间复杂度忽略掉常数项`-1`之后,这个递归算法的时间复杂度依然是O(n)**。对,你没看错,依然是O(n)的时间复杂度! diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index f5803cb4..a88919d4 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 替换数字 @@ -37,11 +35,11 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030165201.png) +![](https://file.kamacoder.com/pics/20231030165201.png) 然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030173058.png) +![](https://file.kamacoder.com/pics/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? @@ -171,7 +169,3 @@ for (int i = 0; i < a.size(); i++) { -

- - - diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index e32f4ce1..4c62312c 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 右旋字符串 @@ -46,16 +44,16 @@ fgabcde 本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106170143.png) +![](https://file.kamacoder.com/pics/20231106170143.png) 右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106171557.png) +![](https://file.kamacoder.com/pics/20231106171557.png) 此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172058.png) +![](https://file.kamacoder.com/pics/20231106172058.png) 其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 @@ -86,7 +84,7 @@ int main() { 可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172534.png) +![](https://file.kamacoder.com/pics/20231106172534.png) 代码如下: @@ -176,7 +174,3 @@ int main() { -

- - - diff --git a/problems/动态规划-股票问题总结篇.md b/problems/动态规划-股票问题总结篇.md index 7927deb7..314fb471 100644 --- a/problems/动态规划-股票问题总结篇.md +++ b/problems/动态规划-股票问题总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # Leetcode股票问题总结篇! @@ -474,7 +472,3 @@ public: -

- - - diff --git a/problems/动态规划总结篇.md b/problems/动态规划总结篇.md index dd1646d6..faca5ecb 100644 --- a/problems/动态规划总结篇.md +++ b/problems/动态规划总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划最强总结篇! @@ -132,7 +130,3 @@ 所以Carl花费的这么大精力,把自己对动规算法理解 一五一十的全部分享给了录友们,帮助大家少走弯路,加油! -

- - - diff --git a/problems/动态规划理论基础.md b/problems/动态规划理论基础.md index 9ffb4533..634f710f 100644 --- a/problems/动态规划理论基础.md +++ b/problems/动态规划理论基础.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划理论基础 @@ -132,7 +130,3 @@ -

- - - diff --git a/problems/双指针总结.md b/problems/双指针总结.md index 409e80ab..9c92e3d6 100644 --- a/problems/双指针总结.md +++ b/problems/双指针总结.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 又是一波总结 @@ -95,7 +93,3 @@ for (int i = 0; i < array.size(); i++) { 建议大家可以把文中涉及到的题目在好好做一做,琢磨琢磨,基本对双指针法就不在话下了。 -

- - - diff --git a/problems/周总结/20201003二叉树周末总结.md b/problems/周总结/20201003二叉树周末总结.md index ea508224..3212ca56 100644 --- a/problems/周总结/20201003二叉树周末总结.md +++ b/problems/周总结/20201003二叉树周末总结.md @@ -254,5 +254,5 @@ traversal(cur->left, tmp, result); * Github:[leetcode-master](https://github.com/youngyangyang04/leetcode-master) * 知乎:[代码随想录](https://www.zhihu.com/people/sun-xiu-yang-64) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/2021013018121150.png) +![](https://file.kamacoder.com/pics/2021013018121150.png)

diff --git a/problems/周总结/20201107回溯周末总结.md b/problems/周总结/20201107回溯周末总结.md index 7e333c76..d910ce25 100644 --- a/problems/周总结/20201107回溯周末总结.md +++ b/problems/周总结/20201107回溯周末总结.md @@ -31,7 +31,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)第一个树形结构没有画出startIndex的作用,**这里这里纠正一下,准确的树形结构如图所示:** -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) ## 周二 @@ -45,7 +45,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123202817973.png) +![40.组合总和II1](https://file.kamacoder.com/pics/20201123202817973.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -79,7 +79,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; **本题的树形结构中,和代码的逻辑有一个小出入,已经判断不是回文的子串就不会进入递归了,纠正如下:** -![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203228309.png) +![131.分割回文串](https://file.kamacoder.com/pics/20201123203228309.png) ## 周四 @@ -90,7 +90,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形图如下: -![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310133532452.png) +![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310133532452.png) 在本文的树形结构图中,我已经把详细的分析思路都画了出来,相信大家看了之后一定会思路清晰不少! @@ -112,7 +112,7 @@ if (s.size() > 12) return result; // 剪枝 如图: -![78.子集](https://code-thinking-1253855093.file.myqcloud.com/pics/202011232041348.png) +![78.子集](https://file.kamacoder.com/pics/202011232041348.png) 认清这个本质之后,今天的题目就是一道模板题了。 diff --git a/problems/周总结/20201112回溯周末总结.md b/problems/周总结/20201112回溯周末总结.md index ec36d121..c2e12284 100644 --- a/problems/周总结/20201112回溯周末总结.md +++ b/problems/周总结/20201112回溯周末总结.md @@ -11,14 +11,14 @@ 树形结构如下: -![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449-20230310133150714.png) +![90.子集II](https://file.kamacoder.com/pics/2020111217110449-20230310133150714.png) ## 周二 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170832333-20230310133155209.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201112170832333-20230310133155209.png) [回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)留言区大家有很多疑问,主要还是和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混合在了一起。 @@ -33,7 +33,7 @@ 可以看出元素1在[1,2]中已经使用过了,但是在[2,1]中还要在使用一次1,所以处理排列问题就不用使用startIndex了。 如图: -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170304979-20230310133201250.png) +![46.全排列](https://file.kamacoder.com/pics/20201112170304979-20230310133201250.png) **大家此时可以感受出排列问题的不同:** @@ -46,7 +46,7 @@ 树形结构如下: -![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112171930470-20230310133206398.png) +![47.全排列II1](https://file.kamacoder.com/pics/20201112171930470-20230310133206398.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -54,11 +54,11 @@ 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172230434-20230310133211392.png) +![47.全排列II2.png](https://file.kamacoder.com/pics/20201112172230434-20230310133211392.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172327967-20230310133216389.png) +![47.全排列II3](https://file.kamacoder.com/pics/20201112172327967-20230310133216389.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** diff --git a/problems/周总结/20201203贪心周末总结.md b/problems/周总结/20201203贪心周末总结.md index 9209efda..4ab1cddb 100644 --- a/problems/周总结/20201203贪心周末总结.md +++ b/problems/周总结/20201203贪心周末总结.md @@ -15,7 +15,7 @@ 如图: -![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858.png) +![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858.png) ## 周二 @@ -31,7 +31,7 @@ 如图: -![55.跳跃游戏](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124154758229.png) +![55.跳跃游戏](https://file.kamacoder.com/pics/20201124154758229.png) ## 周三 @@ -44,7 +44,7 @@ 如图: -![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103-20230310133110942.png) +![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103-20230310133110942.png) 注意:**图中的移动下标是到当前这步覆盖的最远距离(下标2的位置),此时没有到终点,只能增加第二步来扩大覆盖范围**。 @@ -55,10 +55,10 @@ 而版本二就比较统一的,超过范围,步数就加一,但在移动下标的范围了做了文章。 即如果覆盖最远距离下标是倒数第二点:直接加一就行,默认一定可以到终点。如图: -![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286-20230310133115650.png) +![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286-20230310133115650.png) 如果覆盖最远距离下标不是倒数第二点,说明本次覆盖已经到终点了。如图: -![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693-20230310133120115.png) +![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693-20230310133120115.png) 有的录友认为版本一好理解,有的录友认为版本二好理解,其实掌握一种就可以了,也不用非要比拼一下代码的简洁性,简洁程度都差不多了。 diff --git a/problems/周总结/20201210复杂度分析周末总结.md b/problems/周总结/20201210复杂度分析周末总结.md index 461219f2..f449995b 100644 --- a/problems/周总结/20201210复杂度分析周末总结.md +++ b/problems/周总结/20201210复杂度分析周末总结.md @@ -77,7 +77,7 @@ 文中从计算机硬件出发,分析计算机的计算性能,然后亲自做实验,整理出数据如下: -![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175-20230310133304038.png) +![程序超时1](https://file.kamacoder.com/pics/20201208231559175-20230310133304038.png) **大家有一个数量级上的概念就可以了!** diff --git a/problems/周总结/20201217贪心周末总结.md b/problems/周总结/20201217贪心周末总结.md index 91f9656f..19a95615 100644 --- a/problems/周总结/20201217贪心周末总结.md +++ b/problems/周总结/20201217贪心周末总结.md @@ -38,7 +38,7 @@ 如图: -![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878-20230310133332759.png) +![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878-20230310133332759.png) 接着在贪心另一边,左孩子大于右孩子,左孩子的糖果就要比右孩子多。 @@ -50,7 +50,7 @@ 局部最优可以推出全局最优。 如图: -![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791-20230310133346127.png) +![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791-20230310133346127.png) ## 周三 diff --git a/problems/周总结/20201224贪心周末总结.md b/problems/周总结/20201224贪心周末总结.md index b37bba80..a8ba7454 100644 --- a/problems/周总结/20201224贪心周末总结.md +++ b/problems/周总结/20201224贪心周末总结.md @@ -9,7 +9,7 @@ 如图: -![452.用最少数量的箭引爆气球](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123101929791-20230310133845522.png) +![452.用最少数量的箭引爆气球](https://file.kamacoder.com/pics/20201123101929791-20230310133845522.png) 模拟射气球的过程,很多同学真的要去模拟了,实时把气球从数组中移走,这么写的话就复杂了,从前向后遍历重复的只要跳过就可以的。 @@ -21,7 +21,7 @@ 如图: -![435.无重叠区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201221201553618.png) +![435.无重叠区间](https://file.kamacoder.com/pics/20201221201553618.png) 细心的同学就发现了,此题和 [贪心算法:用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像。 @@ -71,7 +71,7 @@ public: 如图: -![763.划分字母区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201222191924417-20230310133855435.png) +![763.划分字母区间](https://file.kamacoder.com/pics/20201222191924417-20230310133855435.png) ## 周四 @@ -86,7 +86,7 @@ public: 如图: -![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791-20230310133859587.png) +![56.合并区间](https://file.kamacoder.com/pics/20201223200632791-20230310133859587.png) ## 总结 diff --git a/problems/周总结/20210114动规周末总结.md b/problems/周总结/20210114动规周末总结.md index ebc720ca..1b6bd84b 100644 --- a/problems/周总结/20210114动规周末总结.md +++ b/problems/周总结/20210114动规周末总结.md @@ -31,7 +31,7 @@ for (int i = 1; i < m; i++) { } ``` -![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392-20230310133703294.png) +![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392-20230310133703294.png) ## 周二 @@ -45,7 +45,7 @@ dp[i][j]定义依然是:表示从(0 ,0)出发,到(i, j) 有dp[i][j]条 如图: -![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928-20230310133707783.png) +![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928-20230310133707783.png) 这里难住了不少同学,代码如下: @@ -70,11 +70,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983-20230310133711888.png) +![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983-20230310133711888.png) 对应的dp table 如图: -![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256-20230310133715981.png) +![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256-20230310133715981.png) ## 周三 @@ -111,7 +111,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104173021581-20230310133720552.png) +![343.整数拆分](https://file.kamacoder.com/pics/20210104173021581-20230310133720552.png) @@ -143,7 +143,7 @@ dp数组如何初始化:只需要初始化dp[0]就可以了,推导的基础 n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987-20230310133724531.png) +![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987-20230310133724531.png) ## 总结 diff --git a/problems/周总结/20210121动规周末总结.md b/problems/周总结/20210121动规周末总结.md index 7bae5ca9..dc32891d 100644 --- a/problems/周总结/20210121动规周末总结.md +++ b/problems/周总结/20210121动规周末总结.md @@ -17,7 +17,7 @@ 关于其他几种常用的背包,大家看这张图就了然于胸了: -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407-20230310133624872.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407-20230310133624872.png) 本文用动规五部曲详细讲解了01背包的二维dp数组的实现方法,大家其实可以发现最简单的是推导公式了,推导公式估计看一遍就记下来了,但难就难在确定初始化和遍历顺序上。 @@ -70,7 +70,7 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210118163425129-20230310133630224.jpg) +![动态规划-背包问题4](https://file.kamacoder.com/pics/20210118163425129-20230310133630224.jpg) 最终结果就是dp[2][4]。 @@ -122,7 +122,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769-20230310133634873.png) +![动态规划-背包问题9](https://file.kamacoder.com/pics/20210110103614769-20230310133634873.png) ## 周三 diff --git a/problems/周总结/20210128动规周末总结.md b/problems/周总结/20210128动规周末总结.md index e785af12..8598ec69 100644 --- a/problems/周总结/20210128动规周末总结.md +++ b/problems/周总结/20210128动规周末总结.md @@ -35,7 +35,7 @@ bagSize = (S + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![494.目标和](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274-20230310132918821.jpg) +![494.目标和](https://file.kamacoder.com/pics/20210125120743274-20230310132918821.jpg) ## 周二 @@ -73,7 +73,7 @@ dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1); 最后dp数组的状态如下所示: -![474.一和零](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120111201512-20230310132936011.jpg) +![474.一和零](https://file.kamacoder.com/pics/20210120111201512-20230310132936011.jpg) ## 周三 diff --git a/problems/周总结/20210225动规周末总结.md b/problems/周总结/20210225动规周末总结.md index 26d805bb..309f277f 100644 --- a/problems/周总结/20210225动规周末总结.md +++ b/problems/周总结/20210225动规周末总结.md @@ -30,7 +30,7 @@ dp[1] = max(nums[0], nums[1]); 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115-20230310133425353.jpg) +![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115-20230310133425353.jpg) 红框dp[nums.size() - 1]为结果。 @@ -42,15 +42,15 @@ dp[1] = max(nums[0], nums[1]); * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643.jpg) +![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374.jpg) +![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491.jpg) +![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491.jpg) 需要注意的是,**“考虑” 不等于 “偷”**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素!对于情况三,取nums[1] 和 nums[3]就是最大的。 @@ -178,7 +178,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129181331613.jpg) +![337.打家劫舍III](https://file.kamacoder.com/pics/20210129181331613.jpg) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 diff --git a/problems/周总结/20210304动规周末总结.md b/problems/周总结/20210304动规周末总结.md index ec442a39..5d84fb19 100644 --- a/problems/周总结/20210304动规周末总结.md +++ b/problems/周总结/20210304动规周末总结.md @@ -77,7 +77,7 @@ dp[0][4] = 0; 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295.png) +![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295.png) 可以看到红色框为最后两次卖出的状态。 @@ -144,7 +144,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221-20230310133805763.png) +![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221-20230310133805763.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 @@ -197,7 +197,7 @@ vector> dp(n, vector(3, 0)); 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229163725348.png) +![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/20201229163725348.png) 最后两个状态 不持有股票(能购买) 和 不持有股票(冷冻期)都有可能最后结果,取最大的。 diff --git a/problems/哈希表总结.md b/problems/哈希表总结.md index cde23ad1..55caffe4 100644 --- a/problems/哈希表总结.md +++ b/problems/哈希表总结.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 哈希表总结篇如约而至 @@ -126,7 +124,3 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 -

- - - diff --git a/problems/哈希表理论基础.md b/problems/哈希表理论基础.md index 825c4657..92b590bc 100644 --- a/problems/哈希表理论基础.md +++ b/problems/哈希表理论基础.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -20,7 +18,7 @@ 哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素,如下图所示: -![哈希表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104234805168.png) +![哈希表1](https://file.kamacoder.com/pics/20210104234805168.png) 那么哈希表能解决什么问题呢,**一般哈希表都是用来快速判断一个元素是否出现集合里。** @@ -38,7 +36,7 @@ 哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。 -![哈希表2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021010423484818.png) +![哈希表2](https://file.kamacoder.com/pics/2021010423484818.png) 如果hashCode得到的数值大于 哈希表的大小了,也就是大于tableSize了,怎么办呢? @@ -54,7 +52,7 @@ 如图所示,小李和小王都映射到了索引下标 1 的位置,**这一现象叫做哈希碰撞**。 -![哈希表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021010423494884.png) +![哈希表3](https://file.kamacoder.com/pics/2021010423494884.png) 一般哈希碰撞有两种解决方法, 拉链法和线性探测法。 @@ -62,7 +60,7 @@ 刚刚小李和小王在索引1的位置发生了冲突,发生冲突的元素都被存储在链表中。 这样我们就可以通过索引找到小李和小王了 -![哈希表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235015226.png) +![哈希表4](https://file.kamacoder.com/pics/20210104235015226.png) (数据规模是dataSize, 哈希表的大小为tableSize) @@ -74,7 +72,7 @@ 例如冲突的位置,放了小李,那么就向下找一个空位放置小王的信息。所以要求tableSize一定要大于dataSize ,要不然哈希表上就没有空置的位置来存放 冲突的数据了。如图所示: -![哈希表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235109950.png) +![哈希表5](https://file.kamacoder.com/pics/20210104235109950.png) 其实关于哈希碰撞还有非常多的细节,感兴趣的同学可以再好好研究一下,这里我就不再赘述了。 @@ -119,7 +117,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 实际上功能都是一样一样的, 但是unordered_set在C++11的时候被引入标准库了,而hash_set并没有,所以建议还是使用unordered_set比较好,这就好比一个是官方认证的,hash_set,hash_map 是C++11标准之前民间高手自发造的轮子。 -![哈希表6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235134572.png) +![哈希表6](https://file.kamacoder.com/pics/20210104235134572.png) ## 总结 @@ -132,7 +130,3 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 -

- - - diff --git a/problems/回溯总结.md b/problems/回溯总结.md index 8d9b78c4..8fd69d51 100644 --- a/problems/回溯总结.md +++ b/problems/回溯总结.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -65,7 +63,7 @@ void backtracking(参数) { 本题我把回溯问题抽象为树形结构,如题: -![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118152928844.png) +![77.组合1](https://file.kamacoder.com/pics/20201118152928844.png) 可以直观的看出其搜索的过程:**for循环横向遍历,递归纵向遍历,回溯不断调整结果集**,这个理念贯穿整个回溯法系列,也是我做了很多回溯的题目,不断摸索其规律才总结出来的。 @@ -75,7 +73,7 @@ void backtracking(参数) { 优化回溯算法只有剪枝一种方法,在[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中把回溯法代码做了剪枝优化,树形结构如图: -![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118153133458.png) +![77.组合4](https://file.kamacoder.com/pics/20201118153133458.png) 大家可以一目了然剪的究竟是哪里。 @@ -91,11 +89,11 @@ void backtracking(参数) { 在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。 树形结构如图: -![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118201921245.png) +![216.组合总和III](https://file.kamacoder.com/pics/20201118201921245.png) 整体思路还是一样的,本题的剪枝会好想一些,即:**已选元素总和如果已经大于n(题中要求的和)了,那么往后遍历就没有意义了,直接剪掉**,如图: -![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202038240.png) +![216.组合总和III1](https://file.kamacoder.com/pics/20201118202038240.png) 在本题中,依然还可以有一个剪枝,就是[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中提到的,对for循环选择的起始范围的剪枝。 @@ -116,7 +114,7 @@ void backtracking(参数) { **注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路**。 树形结构如下: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) 最后还给出了本题的剪枝优化,如下: @@ -127,7 +125,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 优化后树形结构如下: -![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202115929.png) +![39.组合总和1](https://file.kamacoder.com/pics/20201118202115929.png) #### 组合总和(三) @@ -142,7 +140,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111820220675.png) +![40.组合总和II1](https://file.kamacoder.com/pics/2020111820220675.png) 我在图中将used的变化用橘黄色标注上,**可以看出在candidates[i] == candidates[i - 1]相同的情况下:** @@ -163,7 +161,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202335724.png) +![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201118202335724.png) 如果大家在现场面试的时候,一定要注意各种输入异常的情况,例如本题输入1 * #按键。 @@ -191,7 +189,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202448642.png) +![131.分割回文串](https://file.kamacoder.com/pics/20201118202448642.png) ## 子集问题 @@ -202,7 +200,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 如图: -![78.子集](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202544339.png) +![78.子集](https://file.kamacoder.com/pics/20201118202544339.png) 认清这个本质之后,今天的题目就是一道模板题了。 @@ -229,14 +227,14 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449.png) +![90.子集II](https://file.kamacoder.com/pics/2020111217110449.png) ### 递增子序列 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170832333.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201112170832333.png) 很多同学都会把这道题目和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混在一起。 @@ -245,7 +243,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 我用没有排序的集合{2,1,2,2}来举个例子画一个图,如下: -![90.子集II2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111316440479.png) +![90.子集II2](https://file.kamacoder.com/pics/2020111316440479.png) **相信这个图胜过千言万语的解释了**。 @@ -261,7 +259,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 如图: -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170304979.png) +![46.全排列](https://file.kamacoder.com/pics/20201112170304979.png) **大家此时可以感受出排列问题的不同:** @@ -274,7 +272,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112171930470.png) +![47.全排列II1](https://file.kamacoder.com/pics/20201112171930470.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -282,11 +280,11 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172230434.png) +![47.全排列II2.png](https://file.kamacoder.com/pics/20201112172230434.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172327967.png) +![47.全排列II3](https://file.kamacoder.com/pics/20201112172327967.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** @@ -320,7 +318,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555.png) +![](https://file.kamacoder.com/pics/2020111518065555.png) 本题可以算是一道hard的题目了,关于本题的难点我在文中已经详细列出。 @@ -337,7 +335,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 下面我用一个3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118225433127.png) +![51.N皇后](https://file.kamacoder.com/pics/20201118225433127.png) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -365,7 +363,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790.png) +![37.解数独](https://file.kamacoder.com/pics/2020111720451790.png) 解数独可以说是非常难的题目了,如果还一直停留在一维递归的逻辑中,这道题目可以让大家瞬间崩溃。 @@ -440,7 +438,7 @@ N皇后问题分析: 回溯专题汇聚为一张图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030124742.png) +![](https://file.kamacoder.com/pics/20211030124742.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[莫非毛](https://wx.zsxq.com/dweb2/index/footprint/828844212542),所画,总结的非常好,分享给大家。 @@ -448,7 +446,3 @@ N皇后问题分析: -

- - - diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index 96dfeffe..5c20f562 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 回溯算法去重问题的另一种写法 @@ -17,7 +15,7 @@ 我用没有排序的集合{2,1,2,2}来举例子画一个图,如图: -![90.子集II2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111316440479-20230310121930316.png) +![90.子集II2](https://file.kamacoder.com/pics/2020111316440479-20230310121930316.png) 图中,大家就很明显的看到,子集重复了。 @@ -97,7 +95,7 @@ private: 如图: -![90.子集II1](https://code-thinking-1253855093.file.myqcloud.com/pics/202011131625054.png) +![90.子集II1](https://file.kamacoder.com/pics/202011131625054.png) 可以看出一旦把unordered_set uset放在类成员位置,它控制的就是整棵树,包括树枝。 @@ -709,7 +707,3 @@ impl Solution { ``` -

- - - diff --git a/problems/回溯算法理论基础.md b/problems/回溯算法理论基础.md index 862fb101..d31e9651 100644 --- a/problems/回溯算法理论基础.md +++ b/problems/回溯算法理论基础.md @@ -1,14 +1,12 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 回溯算法理论基础 ## 题目分类 -回溯算法大纲 +回溯算法大纲 ## 算法公开课 @@ -116,7 +114,7 @@ if (终止条件) { 如图: -![回溯算法理论基础](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130173631174.png) +![回溯算法理论基础](https://file.kamacoder.com/pics/20210130173631174.png) 注意图中,我特意举例集合大小和孩子的数量是相等的! @@ -176,7 +174,3 @@ void backtracking(参数) { -

- - - diff --git a/problems/字符串总结.md b/problems/字符串总结.md index 7da97914..460944c5 100644 --- a/problems/字符串总结.md +++ b/problems/字符串总结.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 字符串:总结篇 @@ -124,7 +122,3 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易 -

- - - diff --git a/problems/数组总结篇.md b/problems/数组总结篇.md index 7c2fd947..e29a7bd3 100644 --- a/problems/数组总结篇.md +++ b/problems/数组总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 数组总结篇 @@ -127,7 +125,7 @@ ## 总结 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/数组总结.png) +![](https://file.kamacoder.com/pics/数组总结.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。 @@ -135,7 +133,3 @@ 推荐的题目即使大家之前做过了,再读一遍文章,也会帮助你提炼出解题的精髓所在。 -

- - - diff --git a/problems/数组理论基础.md b/problems/数组理论基础.md index e6d25c15..4000208a 100644 --- a/problems/数组理论基础.md +++ b/problems/数组理论基础.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -42,7 +40,7 @@ 那么二维数组直接上图,大家应该就知道怎么回事了 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240606105522.png) +![](https://file.kamacoder.com/pics/20240606105522.png) **那么二维数组在内存的空间地址是连续的么?** @@ -82,7 +80,7 @@ int main() { 如图: -![数组内存](https://code-thinking-1253855093.file.myqcloud.com/pics/20210310150641186.png) +![数组内存](https://file.kamacoder.com/pics/20210310150641186.png) **所以可以看出在C++中二维数组在地址空间上是连续的**。 @@ -113,13 +111,9 @@ public static void test_arr() { 所以Java的二维数组可能是如下排列的方式: -![算法通关数组3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201214111631844.png) +![算法通关数组3](https://file.kamacoder.com/pics/20201214111631844.png) 这里面试中数组相关的理论知识就介绍完了。 -

- - - diff --git a/problems/栈与队列总结.md b/problems/栈与队列总结.md index df022c77..2d09daeb 100644 --- a/problems/栈与队列总结.md +++ b/problems/栈与队列总结.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 栈与队列总结篇 @@ -160,7 +158,3 @@ cd a/b/c/../../ -

- - - diff --git a/problems/栈与队列理论基础.md b/problems/栈与队列理论基础.md index 3ceb8690..eadd6f6a 100644 --- a/problems/栈与队列理论基础.md +++ b/problems/栈与队列理论基础.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 来看看栈和队列不为人知的一面 @@ -13,7 +11,7 @@ 如图所示: -![栈与队列理论1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235346563.png) +![栈与队列理论1](https://file.kamacoder.com/pics/20210104235346563.png) 那么我这里再列出四个关于栈的问题,大家可以思考一下。以下是以C++为例,使用其他编程语言的同学也对应思考一下,自己使用的编程语言里栈和队列是什么样的。 @@ -48,7 +46,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 来说一说栈,栈先进后出,如图所示: -![栈与队列理论2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235434905.png) +![栈与队列理论2](https://file.kamacoder.com/pics/20210104235434905.png) 栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。 不像是set 或者map 提供迭代器iterator来遍历所有元素。 @@ -61,7 +59,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 从下图中可以看出,栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。 -![栈与队列理论3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235459376.png) +![栈与队列理论3](https://file.kamacoder.com/pics/20210104235459376.png) **我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。** @@ -93,7 +91,3 @@ std::queue> third; // 定义以list为底层容器的队列 -

- - - diff --git a/problems/根据身高重建队列(vector原理讲解).md b/problems/根据身高重建队列(vector原理讲解).md index 70a9a97a..162ee273 100644 --- a/problems/根据身高重建队列(vector原理讲解).md +++ b/problems/根据身高重建队列(vector原理讲解).md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -37,7 +35,7 @@ public: ``` 耗时如下: -![vectorinsert](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218203611181.png) +![vectorinsert](https://file.kamacoder.com/pics/20201218203611181.png) 其直观上来看数组的insert操作是O(n)的,整体代码的时间复杂度是O(n^2)。 @@ -70,7 +68,7 @@ public: 耗时如下: -![使用链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218200756257.png) +![使用链表](https://file.kamacoder.com/pics/20201218200756257.png) 大家都知道对于普通数组,一旦定义了大小就不能改变,例如int a[10];,这个数组a至多只能放10个元素,改不了的。 @@ -97,7 +95,7 @@ for (int i = 0; i < vec.size(); i++) { 就是重新申请一个二倍于原数组大小的数组,然后把数据都拷贝过去,并释放原数组内存。(对,就是这么原始粗暴的方法!) 举一个例子,如图: -![vector原理](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218185902217.png) +![vector原理](https://file.kamacoder.com/pics/20201218185902217.png) 原vector中的size和capicity相同都是3,初始化为1 2 3,此时要push_back一个元素4。 @@ -140,7 +138,7 @@ public: 耗时如下: -![vector手动模拟insert](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218200626718.png) +![vector手动模拟insert](https://file.kamacoder.com/pics/20201218200626718.png) 这份代码就是不让vector动态扩容,全程我们自己模拟insert的操作,大家也可以直观的看出是一个O(n^2)的方法了。 @@ -209,7 +207,3 @@ Go中slice的`append`操作和C++中vector的扩容机制基本相同。 -

- - - diff --git a/problems/算法模板.md b/problems/算法模板.md index 0d32cebb..068806d6 100644 --- a/problems/算法模板.md +++ b/problems/算法模板.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 算法模板 ## 算法模板 @@ -853,7 +851,3 @@ Go: -

- - - diff --git a/problems/背包总结篇.md b/problems/背包总结篇.md index 651a92a8..9ce3fdda 100644 --- a/problems/背包总结篇.md +++ b/problems/背包总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 听说背包问题很难? 这篇总结篇来拯救你了 @@ -13,7 +11,7 @@ 关于这几种常见的背包,其关系如下: -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000726.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20230310000726.png) 通过这个图,可以很清晰分清这几种常见背包之间的关系。 @@ -95,7 +93,7 @@ 背包问题总结: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/背包问题1.jpeg) +![](https://file.kamacoder.com/pics/背包问题1.jpeg) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画结的非常好,分享给大家。 @@ -103,7 +101,3 @@ -

- - - diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index d9b953c0..79751c89 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -23,7 +21,7 @@ 如果这几种背包,分不清,我这里画了一个图,如下: -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407.png) 除此以外其他类型的背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 @@ -79,7 +77,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 如图,二维数组为 dp[i][j]。 -![动态规划-背包问题1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103003361.png) +![动态规划-背包问题1](https://file.kamacoder.com/pics/20210110103003361.png) 那么这里 i 、j、dp[i][j] 分别表示什么呢? @@ -93,7 +91,7 @@ i 来表示物品、j表示背包容量。 我们先看把物品0 放入背包的情况: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730113455.png) +![](https://file.kamacoder.com/pics/20240730113455.png) 背包容量为0,放不下物品0,此时背包里的价值为0。 @@ -105,7 +103,7 @@ i 来表示物品、j表示背包容量。 再看把物品1 放入背包: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730114228.png) +![](https://file.kamacoder.com/pics/20240730114228.png) 背包容量为 0,放不下物品0 或者物品1,此时背包里的价值为0。 @@ -152,7 +150,7 @@ i 来表示物品、j表示背包容量。 推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174246.png) +![](https://file.kamacoder.com/pics/20240730174246.png) 如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 @@ -160,7 +158,7 @@ i 来表示物品、j表示背包容量。 所以 放物品1 的情况 = dp[0][1] + 物品1 的价值,推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174436.png) +![](https://file.kamacoder.com/pics/20240730174436.png) 两种情况,分别是放物品1 和 不放物品1,我们要取最大值(毕竟求的是最大价值) @@ -180,7 +178,7 @@ i 来表示物品、j表示背包容量。 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010304192.png) +![动态规划-背包问题2](https://file.kamacoder.com/pics/2021011010304192.png) 在看其他情况。 @@ -207,7 +205,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 此时dp数组初始化情况如图所示: -![动态规划-背包问题7](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103109140.png) +![动态规划-背包问题7](https://file.kamacoder.com/pics/20210110103109140.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -219,7 +217,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 如图: -![动态规划-背包问题10](https://code-thinking-1253855093.file.myqcloud.com/pics/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) +![动态规划-背包问题10](https://file.kamacoder.com/pics/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) 最后初始化代码如下: @@ -238,7 +236,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 在如下图中,可以看出,有两个遍历的维度:物品与背包重量 -![动态规划-背包问题3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010314055.png) +![动态规划-背包问题3](https://file.kamacoder.com/pics/2021011010314055.png) 那么问题来了,**先遍历 物品还是先遍历背包重量呢?** @@ -279,11 +277,11 @@ for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括正上方向),那么先遍历物品,再遍历背包的过程如图所示: -![动态规划-背包问题5](https://code-thinking-1253855093.file.myqcloud.com/pics/202101101032124.png) +![动态规划-背包问题5](https://file.kamacoder.com/pics/202101101032124.png) 再来看看先遍历背包,再遍历物品呢,如图: -![动态规划-背包问题6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103244701.png) +![动态规划-背包问题6](https://file.kamacoder.com/pics/20210110103244701.png) **大家可以看出,虽然两个for循环遍历的次序不同,但是dp[i][j]所需要的数据就是左上角,根本不影响dp[i][j]公式的推导!** @@ -295,7 +293,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210118163425129.jpg) +![动态规划-背包问题4](https://file.kamacoder.com/pics/20210118163425129.jpg) 最终结果就是dp[2][4]。 @@ -593,7 +591,3 @@ int main() { -

- - - diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index b5862bb5..b6a68960 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划:01背包理论基础(滚动数组) @@ -165,7 +163,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769.png) +![动态规划-背包问题9](https://file.kamacoder.com/pics/20210110103614769.png) 本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: @@ -461,7 +459,3 @@ int main() { ``` -

- - - diff --git a/problems/背包问题完全背包一维.md b/problems/背包问题完全背包一维.md index a8e241c3..be7a5d54 100644 --- a/problems/背包问题完全背包一维.md +++ b/problems/背包问题完全背包一维.md @@ -1,3 +1,6 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 完全背包-一维数组 @@ -51,11 +54,11 @@ for (int i = 1; i < n; i++) { // 遍历物品 遍历物品在外层循环,遍历背包容量在内层循环,状态如图: -![动态规划-完全背包1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104529605.jpg) +![动态规划-完全背包1](https://file.kamacoder.com/pics/20210126104529605.jpg) 遍历背包容量在外层循环,遍历物品在内层循环,状态如图: -![动态规划-完全背包2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729234011.png) +![动态规划-完全背包2](https://file.kamacoder.com/pics/20210729234011.png) 看了这两个图,大家就会理解,完全背包中,两个for循环的先后循序,都不影响计算dp[j]所需要的值(这个值就是下标j之前所对应的dp[j])。 diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index 878efc12..39e7ebe3 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划:关于多重背包,你该了解这些! @@ -237,7 +235,3 @@ print(dp[-1]) -

- - - diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 0cc6e915..cb8db1e0 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 完全背包理论基础-二维DP数组 @@ -68,7 +66,7 @@ 推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126112952.png) +![](https://file.kamacoder.com/pics/20241126112952.png) 如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 @@ -80,7 +78,7 @@ 所以 放物品1 的情况 = dp[1][1] + 物品1 的价值,推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126113104.png) +![](https://file.kamacoder.com/pics/20241126113104.png) (**注意上图和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的区别**,对于理解完全背包很重要) @@ -105,7 +103,7 @@ 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010304192.png) +![动态规划-背包问题2](https://file.kamacoder.com/pics/2021011010304192.png) 在看其他情况。 @@ -134,7 +132,7 @@ for (int j = weight[0]; j <= bagWeight; j++) 此时dp数组初始化情况如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241114161608.png) +![](https://file.kamacoder.com/pics/20241114161608.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -187,7 +185,7 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 以本篇举例数据为例,填满了dp二维数组如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126113752.png) +![](https://file.kamacoder.com/pics/20241126113752.png) 因为 物品0 的性价比是最高的,而且 在完全背包中,每一类物品都有无限个,所以有无限个物品0,既然物品0 性价比最高,当然是优先放物品0。 @@ -316,9 +314,49 @@ print(knapsack(n, bag_weight, weight, value)) ### JavaScript +```js +const readline = require('readline').createInterface({ + input: process.stdin, + output: process.stdout +}); +let input = []; +readline.on('line', (line) => { + input.push(line.trim()); +}); + +readline.on('close', () => { + // 第一行解析 n 和 v + const [n, bagweight] = input[0].split(' ').map(Number); + + /// 剩余 n 行解析重量和价值 + const weight = []; + const value = []; + for (let i = 1; i <= n; i++) { + const [wi, vi] = input[i].split(' ').map(Number); + weight.push(wi); + value.push(vi); + } + + + let dp = Array.from({ length: n }, () => Array(bagweight + 1).fill(0)); + + for (let j = weight[0]; j <= bagweight; j++) { + dp[0][j] = dp[0][j-weight[0]] + value[0]; + } + + for (let i = 1; i < n; i++) { + for (let j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); + } + } + } + + console.log(dp[n - 1][bagweight]); +}); + +``` -

- - - diff --git a/problems/贪心算法总结篇.md b/problems/贪心算法总结篇.md index 14d82151..4c67fb40 100644 --- a/problems/贪心算法总结篇.md +++ b/problems/贪心算法总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 贪心算法总结篇 @@ -130,7 +128,7 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 贪心专题汇聚为一张图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/贪心总结water.png) +![](https://file.kamacoder.com/pics/贪心总结water.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412)所画,总结的非常好,分享给大家。 @@ -151,7 +149,3 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 -

- - - diff --git a/problems/贪心算法理论基础.md b/problems/贪心算法理论基础.md index 6fde2dbb..2d0af879 100644 --- a/problems/贪心算法理论基础.md +++ b/problems/贪心算法理论基础.md @@ -1,15 +1,13 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 关于贪心算法,你该了解这些! 题目分类大纲如下: -贪心算法大纲 +贪心算法大纲 ## 算法公开课 @@ -92,7 +90,3 @@ 最后给出贪心的一般解题步骤,大家可以发现这个解题步骤也是比较抽象的,不像是二叉树,回溯算法,给出了那么具体的解题套路和模板。 -

- - - diff --git a/problems/链表总结篇.md b/problems/链表总结篇.md index 7da0d2de..99bb2abc 100644 --- a/problems/链表总结篇.md +++ b/problems/链表总结篇.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 链表总结篇 @@ -77,7 +75,7 @@ ## 总结 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/链表总结.png) +![](https://file.kamacoder.com/pics/链表总结.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。 @@ -98,7 +96,3 @@ -

- - - diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index d1313807..5305c9a9 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -14,7 +12,7 @@ 链表的入口节点称为链表的头结点也就是head。 如图所示: -![链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194529815.png) +![链表1](https://file.kamacoder.com/pics/20200806194529815.png) ## 链表的类型 @@ -33,7 +31,7 @@ 双链表 既可以向前查询也可以向后查询。 如图所示: -![链表2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194559317.png) +![链表2](https://file.kamacoder.com/pics/20200806194559317.png) ### 循环链表 @@ -41,7 +39,7 @@ 循环链表可以用来解决约瑟夫环问题。 -![链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194629603.png) +![链表4](https://file.kamacoder.com/pics/20200806194629603.png) ## 链表的存储方式 @@ -56,7 +54,7 @@ 如图所示: -![链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194613920.png) +![链表3](https://file.kamacoder.com/pics/20200806194613920.png) 这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。 @@ -106,7 +104,7 @@ head->val = 5; 删除D节点,如图所示: -![链表-删除节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195114541-20230310121459257.png) +![链表-删除节点](https://file.kamacoder.com/pics/20200806195114541-20230310121459257.png) 只要将C节点的next指针 指向E节点就可以了。 @@ -120,7 +118,7 @@ head->val = 5; 如图所示: -![链表-添加节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195134331-20230310121503147.png) +![链表-添加节点](https://file.kamacoder.com/pics/20200806195134331-20230310121503147.png) 可以看出链表的增添和删除都是O(1)操作,也不会影响到其他节点。 @@ -130,7 +128,7 @@ head->val = 5; 再把链表的特性和数组的特性进行一个对比,如图所示: -![链表-链表与数据性能对比](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195200276.png) +![链表-链表与数据性能对比](https://file.kamacoder.com/pics/20200806195200276.png) 数组在定义的时候,长度就是固定的,如果想改动数组的长度,就需要重新定义一个新的数组。 @@ -275,7 +273,3 @@ public class Node -

- - - diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 48944b5e..0207b71e 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 面试题 02.07. 链表相交 @@ -15,7 +13,7 @@ 图示两个链表在节点 c1 开始相交: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) +![](https://file.kamacoder.com/pics/20211219221657.png) 题目数据 保证 整个链式结构中不存在环。 @@ -23,15 +21,15 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) +![](https://file.kamacoder.com/pics/20211219221723.png) 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) +![](https://file.kamacoder.com/pics/20211219221749.png) 示例 3: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) +![](https://file.kamacoder.com/pics/20211219221812.png) @@ -575,7 +573,3 @@ func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? { ``` -

- - -