diff --git a/README.md b/README.md index 1de973a1..b0da9955 100644 --- a/README.md +++ b/README.md @@ -123,19 +123,22 @@ ## 知识星球精选 -1. [选择方向的时候,我也迷茫了](https://mp.weixin.qq.com/s/ZCzFiAHZHLqHPLJQXNm75g) -2. [刷题就用库函数了,怎么了?](https://mp.weixin.qq.com/s/6K3_OSaudnHGq2Ey8vqYfg) -3. [关于实习,大家可能有点迷茫!](https://mp.weixin.qq.com/s/xcxzi7c78kQGjvZ8hh7taA) -4. [马上秋招了,慌得很!](https://mp.weixin.qq.com/s/7q7W8Cb2-a5U5atZdOnOFA) -5. [Carl看了上百份简历,总结了这些!](https://mp.weixin.qq.com/s/sJa87MZD28piCOVMFkIbwQ) -6. [面试中遇到了发散性问题.....](https://mp.weixin.qq.com/s/SSonDxi2pjkSVwHNzZswng) -7. [英语到底重不重要!](https://mp.weixin.qq.com/s/1PRZiyF_-TVA-ipwDNjdKw) -8. [计算机专业要不要读研!](https://mp.weixin.qq.com/s/c9v1L3IjqiXtkNH7sOMAdg) -9. [秋招和提前批都越来越提前了....](https://mp.weixin.qq.com/s/SNFiRDx8CKyjhTPlys6ywQ) -10. [你的简历里「专业技能」写的够专业么?](https://mp.weixin.qq.com/s/bp6y-e5FVN28H9qc8J9zrg) -11. [对于秋招,实习生也有烦恼....](https://mp.weixin.qq.com/s/ka07IPryFnfmIjByFFcXDg) -12. [华为提前批已经开始了.....](https://mp.weixin.qq.com/s/OC35QDG8pn5OwLpCxieStw) -13. [大厂新人培养体系应该是什么样的?](https://mp.weixin.qq.com/s/WBaPCosOljB5NEkFL2GhOQ) +* [HR面注意事项](https://mp.weixin.qq.com/s/0mDyCyCBfa0DeGov3Pebnw) +* [刷题攻略要刷两遍!](https://mp.weixin.qq.com/s/e3_L7FZglY4UlTVvKolZyQ) +* [秋招进行中的迷茫与焦虑......](https://mp.weixin.qq.com/s/X15MUw4sfH_AQNHdAivEvg) +* [大厂新人培养体系应该是什么样的?](https://mp.weixin.qq.com/s/WBaPCosOljB5NEkFL2GhOQ) +* [你的简历里「专业技能」写的够专业么?](https://mp.weixin.qq.com/s/bp6y-e5FVN28H9qc8J9zrg) +* [Carl看了上百份简历,总结了这些!](https://mp.weixin.qq.com/s/sJa87MZD28piCOVMFkIbwQ) +* [备战2022届秋招](https://mp.weixin.qq.com/s/7q7W8Cb2-a5U5atZdOnOFA) +* [技术不太好,如果选择方向](https://mp.weixin.qq.com/s/ZCzFiAHZHLqHPLJQXNm75g) +* [刷题要不要使用库函数](https://mp.weixin.qq.com/s/6K3_OSaudnHGq2Ey8vqYfg) +* [关于实习的几点问题](https://mp.weixin.qq.com/s/xcxzi7c78kQGjvZ8hh7taA) +* [面试中遇到了发散性问题,怎么帮?](https://mp.weixin.qq.com/s/SSonDxi2pjkSVwHNzZswng) +* [英语到底重不重要!](https://mp.weixin.qq.com/s/1PRZiyF_-TVA-ipwDNjdKw) +* [计算机专业要不要读研!](https://mp.weixin.qq.com/s/c9v1L3IjqiXtkNH7sOMAdg) +* [关于提前批的一些建议](https://mp.weixin.qq.com/s/SNFiRDx8CKyjhTPlys6ywQ) +* [已经在实习的录友要如何准备秋招](https://mp.weixin.qq.com/s/ka07IPryFnfmIjByFFcXDg) +* [华为提前批已经开始了](https://mp.weixin.qq.com/s/OC35QDG8pn5OwLpCxieStw) ## 杂谈 diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 5be94996..f8c9da5f 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -62,7 +62,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 C++代码: -```C++ +```CPP class Solution { public: vector twoSum(vector& nums, int target) { @@ -210,4 +210,4 @@ function twoSum(array $nums, int $target): array * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 0063b358..0f4cb2f6 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -67,7 +67,7 @@ 以上三种情况分析完了,那么递归公式如下: -```C++ +```CPP if (s[i] == s[j]) { if (j - i <= 1) { // 情况一 和 情况二 dp[i][j] = true; @@ -81,7 +81,7 @@ if (s[i] == s[j]) { 在得到[i,j]区间是否是回文子串的时候,直接保存最长回文子串的左边界和右边界,代码如下: -```C++ +```CPP if (s[i] == s[j]) { if (j - i <= 1) { // 情况一 和 情况二 dp[i][j] = true; @@ -120,7 +120,7 @@ dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: 代码如下: -```C++ +```CPP for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 for (int j = i; j < s.size(); j++) { if (s[i] == s[j]) { @@ -150,7 +150,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: string longestPalindrome(string s) { @@ -181,7 +181,7 @@ public: ``` 以上代码是为了凸显情况一二三,当然是可以简洁一下的,如下: -```C++ +```CPP class Solution { public: string longestPalindrome(string s) { @@ -226,7 +226,7 @@ public: **这两种情况可以放在一起计算,但分别计算思路更清晰,我倾向于分别计算**,代码如下: -```C++ +```CPP class Solution { public: int left = 0; @@ -286,6 +286,6 @@ public: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 36abb58c..9a3a8d5b 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -46,7 +46,7 @@ https://leetcode-cn.com/problems/3sum/ 大家可以尝试使用哈希法写一写,就知道其困难的程度了。 哈希法C++代码: -```C++ +```CPP class Solution { public: vector> threeSum(vector& nums) { @@ -107,7 +107,7 @@ public: C++代码代码如下: -```C++ +```CPP class Solution { public: vector> threeSum(vector& nums) { @@ -399,4 +399,4 @@ function threeSum(array $nums): array * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 1562052c..7aa72e1b 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -416,4 +416,4 @@ var letterCombinations = function(digits) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 4a8fa947..75cd9e8e 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -67,7 +67,7 @@ https://leetcode-cn.com/problems/4sum/ C++代码 -```C++ +```CPP class Solution { public: vector> fourSum(vector& nums, int target) { @@ -289,4 +289,4 @@ var fourSum = function(nums, target) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 4b17c972..3e1a682c 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -58,7 +58,7 @@ 此时不难写出如下C++代码: -```C++ +```CPP class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { @@ -208,4 +208,4 @@ fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 178bc6f8..a26aa308 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -108,7 +108,7 @@ cd a/b/c/../../ 实现C++代码如下: -```C++ +```CPP class Solution { public: bool isValid(string s) { @@ -264,4 +264,4 @@ var isValid = function(s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index cae69cea..91e566dd 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -43,7 +43,7 @@ https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 对应的C++代码实现如下: (注释中详细和如上图中的三步做对应) -```C++ +```CPP class Solution { public: ListNode* swapPairs(ListNode* head) { @@ -254,4 +254,4 @@ fun swapPairs(head: ListNode?): ListNode? { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index c9bab0ad..3819d6a5 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -48,7 +48,7 @@ 代码如下: -```C++ +```CPP // 时间复杂度:O(n^2) // 空间复杂度:O(1) class Solution { @@ -85,7 +85,7 @@ public: 后序都会一一介绍到,本题代码如下: -```C++ +```CPP // 时间复杂度:O(n) // 空间复杂度:O(1) class Solution { @@ -239,4 +239,4 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index fc14a34a..4fb418f0 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -315,7 +315,7 @@ next[i] = j; 最后整体构建next数组的函数代码如下: -```C++ +```CPP void getNext(int* next, const string& s){     int j = -1;     next[0] = j; @@ -386,7 +386,7 @@ if (j == (t.size() - 1) ) { 那么使用next数组,用模式串匹配文本串的整体代码如下: -```C++ +```CPP int j = -1; // 因为next数组里记录的起始位置为-1 for (int i = 0; i < s.size(); i++) { // 注意i就从0开始     while(j >= 0 && s[i] != t[j + 1]) { // 不匹配 @@ -405,7 +405,7 @@ for (int i = 0; i < s.size(); i++) { // 注意i就从0开始 # 前缀表统一减一 C++代码实现 -```C++ +```CPP class Solution { public:     void getNext(int* next, const string& s) { @@ -457,7 +457,7 @@ public: 我给出的getNext的实现为:(前缀表统一减一) -```C++ +```CPP void getNext(int* next, const string& s) {     int j = -1;     next[0] = j; @@ -479,7 +479,7 @@ void getNext(int* next, const string& s) { 那么前缀表不减一来构建next数组,代码如下: -```C++ +```CPP void getNext(int* next, const string& s) { int j = 0; next[0] = 0; @@ -502,7 +502,7 @@ void getNext(int* next, const string& s) { 实现代码如下: -```C++ +```CPP class Solution { public: void getNext(int* next, const string& s) { @@ -902,4 +902,4 @@ var strStr = function (haystack, needle) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 53e6644d..10ac9df4 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -75,7 +75,7 @@ 对应的C++代码如下: -```C++ +```CPP class Solution { public: void nextPermutation(vector& nums) { @@ -138,5 +138,5 @@ class Solution { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index cb6fa2e9..f83de1a8 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -64,7 +64,7 @@ 可以写出如下代码 -```C++ +```CPP // 二分查找,寻找target的右边界(不包括target) // 如果rightBorder为没有被赋值(即target在数组范围的左边,例如数组[3,3],target为2),为了处理情况一 int getRightBorder(vector& nums, int target) { @@ -86,7 +86,7 @@ int getRightBorder(vector& nums, int target) { ## 寻找左边界 -```C++ +```CPP // 二分查找,寻找target的左边界leftBorder(不包括target) // 如果leftBorder没有被赋值(即target在数组范围的右边,例如数组[3,3],target为4),为了处理情况一 int getLeftBorder(vector& nums, int target) { @@ -110,7 +110,7 @@ int getLeftBorder(vector& nums, int target) { 左右边界计算完之后,看一下主体代码,这里把上面讨论的三种情况,都覆盖了 -```C++ +```CPP class Solution { public: vector searchRange(vector& nums, int target) { @@ -402,5 +402,5 @@ class Solution: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 9bd9ef16..4ad8a80c 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -116,7 +116,7 @@ public: **大家要仔细看注释,思考为什么要写while(left <= right), 为什么要写right = middle - 1**。 -```C++ +```CPP class Solution { public: int searchInsert(vector& nums, int target) { @@ -158,7 +158,7 @@ public: **大家要仔细看注释,思考为什么要写while (left < right), 为什么要写right = middle**。 -```C++ +```CPP class Solution { public: int searchInsert(vector& nums, int target) { @@ -313,4 +313,4 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index e43708b8..64be7013 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -90,7 +90,7 @@ bool backtracking(vector>& board) 代码如下:(**详细看注释**) -```C++ +```CPP bool backtracking(vector>& board) { for (int i = 0; i < board.size(); i++) { // 遍历行 for (int j = 0; j < board[0].size(); j++) { // 遍历列 @@ -125,7 +125,7 @@ bool backtracking(vector>& board) { 代码如下: -```C++ +```CPP bool isValid(int row, int col, char val, vector>& board) { for (int i = 0; i < 9; i++) { // 判断行里是否重复 if (board[row][i] == val) { @@ -154,7 +154,7 @@ bool isValid(int row, int col, char val, vector>& board) { ## C++代码 -```C++ +```CPP class Solution { private: bool backtracking(vector>& board) { @@ -437,4 +437,4 @@ var solveSudoku = function(board) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index ba8128b5..0c452e22 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -73,7 +73,7 @@ candidates 中的数字可以无限制重复被选取。 代码如下: -```C++ +```CPP vector> result; vector path; void backtracking(vector& candidates, int target, int sum, int startIndex) @@ -89,7 +89,7 @@ void backtracking(vector& candidates, int target, int sum, int startIndex) sum等于target的时候,需要收集结果,代码如下: -```C++ +```CPP if (sum > target) { return; } @@ -107,7 +107,7 @@ if (sum == target) { 如何重复选取呢,看代码,注释部分: -```C++ +```CPP for (int i = startIndex; i < candidates.size(); i++) { sum += candidates[i]; path.push_back(candidates[i]); @@ -119,7 +119,7 @@ for (int i = startIndex; i < candidates.size(); i++) { 按照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中给出的模板,不难写出如下C++完整代码: -```C++ +```CPP // 版本一 class Solution { private: @@ -179,7 +179,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 整体代码如下:(注意注释的部分) -```C++ +```CPP class Solution { private: vector> result; @@ -351,4 +351,4 @@ var combinationSum = function(candidates, target) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 70e012da..5ea11ca7 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -90,7 +90,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 代码如下: -```C++ +```CPP vector> result; // 存放组合集合 vector path; // 符合条件的组合 void backtracking(vector& candidates, int target, int sum, int startIndex, vector& used) { @@ -102,7 +102,7 @@ void backtracking(vector& candidates, int target, int sum, int startIndex, 代码如下: -```C++ +```CPP if (sum > target) { // 这个条件其实可以省略 return; } @@ -137,7 +137,7 @@ if (sum == target) { 那么单层搜索的逻辑代码如下: -```C++ +```CPP for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) { // used[i - 1] == true,说明同一树支candidates[i - 1]使用过 // used[i - 1] == false,说明同一树层candidates[i - 1]使用过 @@ -161,7 +161,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 回溯三部曲分析完了,整体C++代码如下: -```C++ +```CPP class Solution { private: vector> result; @@ -206,7 +206,7 @@ public: 这里直接用startIndex来去重也是可以的, 就不用used数组了。 -```C++ +```CPP class Solution { private: vector> result; @@ -398,4 +398,4 @@ var combinationSum2 = function(candidates, target) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 3f4d0123..235776a0 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -77,7 +77,7 @@ 一样的方法,只要从头遍历一遍所有的列,然后求出每一列雨水的体积,相加之后就是总雨水的体积了。 首先从头遍历所有的列,并且**要注意第一个柱子和最后一个柱子不接雨水**,代码如下: -```C++ +```CPP for (int i = 0; i < height.size(); i++) { // 第一个柱子和最后一个柱子不接雨水 if (i == 0 || i == height.size() - 1) continue; @@ -86,7 +86,7 @@ for (int i = 0; i < height.size(); i++) { 在for循环中求左右两边最高柱子,代码如下: -```C++ +```CPP int rHeight = height[i]; // 记录右边柱子的最高高度 int lHeight = height[i]; // 记录左边柱子的最高高度 for (int r = i + 1; r < height.size(); r++) { @@ -99,14 +99,14 @@ for (int l = i - 1; l >= 0; l--) { 最后,计算该列的雨水高度,代码如下: -```C++ +```CPP int h = min(lHeight, rHeight) - height[i]; if (h > 0) sum += h; // 注意只有h大于零的时候,在统计到总和中 ``` 整体代码如下: -```C++ +```CPP class Solution { public: int trap(vector& height) { @@ -152,7 +152,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: int trap(vector& height) { @@ -287,7 +287,7 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况 求当前凹槽雨水的体积代码如下: -```C++ +```CPP while (!st.empty() && height[i] > height[st.top()]) { // 注意这里是while,持续跟新栈顶元素 int mid = st.top(); st.pop(); @@ -301,7 +301,7 @@ while (!st.empty() && height[i] > height[st.top()]) { // 注意这里是while, 关键部分讲完了,整体代码如下: -```C++ +```CPP class Solution { public: int trap(vector& height) { @@ -335,7 +335,7 @@ public: 以上代码冗余了一些,但是思路是清晰的,下面我将代码精简一下,如下: -```C++ +```CPP class Solution { public: int trap(vector& height) { @@ -398,4 +398,4 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index a161d944..d946315b 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -63,7 +63,7 @@ C++代码如下:(详细注释) -```C++ +```CPP // 版本一 class Solution { public: @@ -106,7 +106,7 @@ public: 代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -236,4 +236,4 @@ var jump = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 3b3ba180..c0626fb7 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -106,7 +106,7 @@ for (int i = 0; i < nums.size(); i++) { 整体C++代码如下: -```C++ +```CPP class Solution { public: vector> result; @@ -289,4 +289,4 @@ var permute = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index c8764241..61428299 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -338,4 +338,4 @@ func backTring(nums,subRes []int,res *[][]int,used []bool){ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index a8919ec3..387e75c3 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -171,7 +171,7 @@ bool isValid(int row, int col, vector& chessboard, int n) { ## C++代码 -```C++ +```CPP class Solution { private: vector> result; @@ -490,4 +490,4 @@ var solveNQueens = function(n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 81e0b35a..cba7d6c7 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -25,7 +25,7 @@ 时间复杂度:O(n^2) 空间复杂度:O(1) -```C++ +```CPP class Solution { public: int maxSubArray(vector& nums) { @@ -81,7 +81,7 @@ if (count > result) result = count; 那么不难写出如下C++代码(关键地方已经注释) -```C++ +```CPP class Solution { public: int maxSubArray(vector& nums) { @@ -109,7 +109,7 @@ public: 那么先给出我的dp代码如下,有时间的录友可以提前做一做: -```C++ +```CPP class Solution { public: int maxSubArray(vector& nums) { @@ -214,4 +214,4 @@ var maxSubArray = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md index 20a8c66c..0932c77f 100644 --- a/problems/0053.最大子序和(动态规划).md +++ b/problems/0053.最大子序和(动态规划).md @@ -65,7 +65,7 @@ dp[0]应该是多少呢? 以上动规五部曲分析完毕,完整代码如下: -```C++ +```CPP class Solution { public: int maxSubArray(vector& nums) { @@ -191,4 +191,4 @@ const maxSubArray = nums => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 8618515e..0b1c1d36 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -58,7 +58,7 @@ i每次移动只能在cover的范围内移动,每移动一个元素,cover得 C++代码如下: -```C++ +```CPP class Solution { public: bool canJump(vector& nums) { @@ -161,4 +161,4 @@ var canJump = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index d4ffc554..99c870c6 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -56,7 +56,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: // 按照区间左边界从小到大排序 @@ -92,7 +92,7 @@ public: 当然以上代码有冗余一些,可以优化一下,如下:(思路是一样的) -```C++ +```CPP class Solution { public: vector> merge(vector>& intervals) { @@ -229,4 +229,4 @@ var merge = function (intervals) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 90b74005..e46dae6d 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -66,7 +66,7 @@ 整体C++代码如下: -```C++ +```CPP class Solution { public: vector> generateMatrix(int n) { @@ -309,4 +309,4 @@ func generateMatrix(n int) [][]int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 50e70b3e..84c84075 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -59,7 +59,7 @@ 此时问题就可以转化为求二叉树叶子节点的个数,代码如下: -```C++ +```CPP class Solution { private: int dfs(int i, int j, int m, int n) { @@ -128,7 +128,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 以上动规五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int uniquePaths(int m, int n) { @@ -149,7 +149,7 @@ public: 其实用一个一维数组(也可以理解是滚动数组)就可以了,但是不利于理解,可以优化点空间,建议先理解了二维,在理解一维,C++代码如下: -```C++ +```CPP class Solution { public: int uniquePaths(int m, int n) { @@ -187,7 +187,7 @@ public: 例如如下代码是不行的。 -```C++ +```CPP class Solution { public: int uniquePaths(int m, int n) { @@ -204,7 +204,7 @@ public: 需要在计算分子的时候,不断除以分母,代码如下: -```C++ +```CPP class Solution { public: int uniquePaths(int m, int n) { @@ -333,4 +333,4 @@ var uniquePaths = function(m, n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index a61ffd02..3df1b301 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -97,7 +97,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 所以本题初始化代码为: -```C++ +```CPP vector> dp(m, vector(n, 0)); for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1; for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1; @@ -111,7 +111,7 @@ for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1; 代码如下: -```C++ +```CPP for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (obstacleGrid[i][j] == 1) continue; @@ -135,7 +135,7 @@ for (int i = 1; i < m; i++) { 动规五部分分析完毕,对应C++代码如下: -```C++ +```CPP class Solution { public: int uniquePathsWithObstacles(vector>& obstacleGrid) { @@ -341,4 +341,4 @@ var uniquePathsWithObstacles = function(obstacleGrid) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 96899d37..bb9a757c 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -109,7 +109,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 以上五部分析完之后,C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -130,7 +130,7 @@ public: 当然依然也可以,优化一下空间复杂度,代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -163,7 +163,7 @@ public: 这里我先给出我的实现代码: -```C++ +```CPP class Solution { public: int climbStairs(int n) { @@ -301,4 +301,4 @@ var climbStairs = function(n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 5c8270b6..a2a5ca32 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -192,4 +192,4 @@ func climbStairs(n int) int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 3a3b584a..d7b6438d 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -123,7 +123,7 @@ if (word1[i - 1] != word2[j - 1]) 递归公式代码如下: -```C++ +```CPP if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } @@ -151,7 +151,7 @@ dp[i][0] :以下标i-1为结尾的字符串word1,和空字符串word2,最 所以C++代码如下: -```C++ +```CPP for (int i = 0; i <= word1.size(); i++) dp[i][0] = i; for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; ``` @@ -175,7 +175,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 代码如下: -```C++ +```CPP for (int i = 1; i <= word1.size(); i++) { for (int j = 1; j <= word2.size(); j++) { if (word1[i - 1] == word2[j - 1]) { @@ -198,7 +198,7 @@ for (int i = 1; i <= word1.size(); i++) { 以上动规五部分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int minDistance(string word1, string word2) { @@ -338,4 +338,4 @@ const minDistance = (word1, word2) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 0b289a40..d88a5711 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -173,7 +173,7 @@ for循环每次从startIndex开始遍历,然后用path保存取到的节点i 代码如下: -```C++ +```CPP for (int i = startIndex; i <= n; i++) { // 控制树的横向遍历 path.push_back(i); // 处理节点 backtracking(n, k, i + 1); // 递归:控制树的纵向遍历,注意下一层搜索要从i+1开始 @@ -188,7 +188,7 @@ backtracking的下面部分就是回溯的操作了,撤销本次处理的结 关键地方都讲完了,组合问题C++完整代码如下: -```C++ +```CPP class Solution { private: vector> result; // 存放符合条件结果的集合 @@ -440,4 +440,4 @@ func backtrack(n,k,start int,track []int){ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index d3e82f09..d545543d 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -249,4 +249,4 @@ var combine = function(n, k) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 0b2f3c09..ca49f97b 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -120,7 +120,7 @@ void backtracking(参数) { 可以写出如下回溯算法C++代码: -```C++ +```CPP class Solution { private: vector> result; @@ -268,4 +268,4 @@ var subsets = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index 1abe6623..f0f58c0f 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -23,7 +23,7 @@ ## 双指针解法 -```C++ +```CPP class Solution { public: int largestRectangleArea(vector& heights) { @@ -56,7 +56,7 @@ public: 所以需要循环查找,也就是下面在寻找的过程中使用了while,详细请看下面注释,整理思路在题解:[42. 接雨水](https://mp.weixin.qq.com/s/QogENxhotboct9wn7GgYUw)中已经介绍了。 -```C++ +```CPP class Solution { public: int largestRectangleArea(vector& heights) { @@ -125,7 +125,7 @@ public: C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -164,7 +164,7 @@ public: 代码精简之后: -```C++ +```CPP // 版本二 class Solution { public: @@ -191,3 +191,4 @@ public: 这里我依然建议大家按部就班把版本一写出来,把情况一二三分析清楚,然后在精简代码到版本二。 直接看版本二容易忽略细节! +
diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 71aef5c7..5c19adc0 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -124,7 +124,7 @@ public: 代码如下: -```C++ +```CPP class Solution { private: vector> result; @@ -288,4 +288,4 @@ var subsetsWithDup = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 40ad7684..9a95af22 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -183,7 +183,7 @@ void backtracking(参数) { 可以写出如下回溯算法C++代码: -```C++ +```CPP class Solution { private: vector result;// 记录结果 @@ -453,4 +453,4 @@ func isNormalIp(s string,startIndex,end int)bool{ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index 56f50d46..7cbe4fec 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -103,7 +103,7 @@ j相当于是头结点的元素,从1遍历到i为止。 代码如下: -```C++ +```CPP for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { dp[i] += dp[j - 1] * dp[i - j]; @@ -123,7 +123,7 @@ n为5时候的dp数组状态如图: 综上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int numTrees(int n) { @@ -234,4 +234,4 @@ const numTrees =(n) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index a7634849..d6db901e 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -32,7 +32,7 @@ 可以递归中序遍历将二叉搜索树转变成一个数组,代码如下: -```C++ +```CPP vector vec; void traversal(TreeNode* root) { if (root == NULL) return; @@ -44,7 +44,7 @@ void traversal(TreeNode* root) { 然后只要比较一下,这个数组是否是有序的,**注意二叉搜索树中不能有重复元素**。 -```C++ +```CPP traversal(root); for (int i = 1; i < vec.size(); i++) { // 注意要小于等于,搜索树里不能有相同元素 @@ -55,7 +55,7 @@ return true; 整体代码如下: -```C++ +```CPP class Solution { private: vector vec; @@ -163,7 +163,7 @@ return left && right; 整体代码如下: -```C++ +```CPP class Solution { public: long long maxVal = LONG_MIN; // 因为后台测试数据中有int最小值 @@ -189,7 +189,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: TreeNode* pre = NULL; // 用来记录前一个节点 @@ -214,7 +214,7 @@ public: 迭代法中序遍历稍加改动就可以了,代码如下: -```C++ +```CPP class Solution { public: bool isValidBST(TreeNode* root) { @@ -533,4 +533,4 @@ var isValidBST = function (root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index 31a68692..e4eb412a 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -61,7 +61,7 @@ bool compare(TreeNode* tree1, TreeNode* tree2) 此时tree1、tree2节点不为空,且数值也不相同的情况我们也处理了。 代码如下: -```C++ +```CPP if (tree1 == NULL && tree2 != NULL) return false; else if (tree1 != NULL && tree2 == NULL) return false; else if (tree1 == NULL && tree2 == NULL) return true; @@ -77,7 +77,7 @@ else if (tree1->val != tree2->val) return false; // 注意这里我没有 代码如下: -```C++ +```CPP bool left = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左 bool right = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右 bool isSame = left && right; // 左子树:中、 右子树:中(逻辑处理) @@ -85,7 +85,7 @@ return isSame; ``` 最后递归的C++整体代码如下: -```C++ +```CPP class Solution { public: bool compare(TreeNode* tree1, TreeNode* tree2) { @@ -119,7 +119,7 @@ public: ## 递归 -```C++ +```CPP class Solution { public: bool compare(TreeNode* left, TreeNode* right) { @@ -138,7 +138,7 @@ public: ## 迭代法 -```C++ +```CPP class Solution { public: @@ -246,5 +246,5 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 9f3ceb73..e091844b 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -73,7 +73,7 @@ bool compare(TreeNode* left, TreeNode* right) 此时左右节点不为空,且数值也不相同的情况我们也处理了。 代码如下: -```C++ +```CPP if (left == NULL && right != NULL) return false; else if (left != NULL && right == NULL) return false; else if (left == NULL && right == NULL) return true; @@ -93,7 +93,7 @@ else if (left->val != right->val) return false; // 注意这里我没有 代码如下: -```C++ +```CPP bool outside = compare(left->left, right->right); // 左子树:左、 右子树:右 bool inside = compare(left->right, right->left); // 左子树:右、 右子树:左 bool isSame = outside && inside; // 左子树:中、 右子树:中(逻辑处理) @@ -104,7 +104,7 @@ return isSame; 最后递归的C++整体代码如下: -```C++ +```CPP class Solution { public: bool compare(TreeNode* left, TreeNode* right) { @@ -137,7 +137,7 @@ public: **盲目的照着抄,结果就是:发现这是一道“简单题”,稀里糊涂的就过了,但是真正的每一步判断逻辑未必想到清楚。** 当然我可以把如上代码整理如下: -```C++ +```CPP class Solution { public: bool compare(TreeNode* left, TreeNode* right) { @@ -177,7 +177,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: bool isSymmetric(TreeNode* root) { @@ -212,7 +212,7 @@ public: 只要把队列原封不动的改成栈就可以了,我下面也给出了代码。 -```C++ +```CPP class Solution { public: bool isSymmetric(TreeNode* root) { @@ -581,4 +581,4 @@ var isSymmetric = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5f1721bf..c38cc796 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -60,7 +60,7 @@ C++代码: -```C++ +```CPP class Solution { public: vector> levelOrder(TreeNode* root) { @@ -245,7 +245,7 @@ var levelOrder = function(root) { C++代码: -```C++ +```CPP class Solution { public: vector> levelOrderBottom(TreeNode* root) { @@ -422,7 +422,7 @@ var levelOrderBottom = function(root) { C++代码: -```C++ +```CPP class Solution { public: vector rightSideView(TreeNode* root) { @@ -599,7 +599,7 @@ var rightSideView = function(root) { C++代码: -```C++ +```CPP class Solution { public: vector averageOfLevels(TreeNode* root) { @@ -794,7 +794,7 @@ var averageOfLevels = function(root) { C++代码: -```C++ +```CPP class Solution { public: vector> levelOrder(Node* root) { @@ -1003,7 +1003,7 @@ var levelOrder = function(root) { C++代码: -```C++ +```CPP class Solution { public: vector largestValues(TreeNode* root) { @@ -1147,7 +1147,7 @@ struct Node { C++代码: -```C++ +```CPP class Solution { public: Node* connect(Node* root) { @@ -1269,7 +1269,7 @@ func connect(root *Node) *Node { C++代码: -```C++ +```CPP class Solution { public: Node* connect(Node* root) { @@ -1451,7 +1451,7 @@ func connect(root *Node) *Node { C++代码如下: -```C++ +```CPP class Solution { public: int maxDepth(TreeNode* root) { @@ -1491,7 +1491,7 @@ JavaScript: 代码如下:(详细注释) -```C++ +```CPP class Solution { public: int minDepth(TreeNode* root) { @@ -1554,4 +1554,4 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 252f9e61..f09361e1 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -527,4 +527,4 @@ var maxdepth = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 4bdc228d..4cfe2858 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -59,7 +59,7 @@ 不难写出如下代码:(先把框架写出来) -```C++ +```CPP TreeNode* traversal (vector& inorder, vector& postorder) { // 第一步 @@ -155,7 +155,7 @@ root->right = traversal(rightInorder, rightPostorder); ### C++完整代码 -```C++ +```CPP class Solution { private: TreeNode* traversal (vector& inorder, vector& postorder) { @@ -209,7 +209,7 @@ public: 加了日志的代码如下:(加了日志的代码不要在leetcode上提交,容易超时) -```C++ +```CPP class Solution { private: TreeNode* traversal (vector& inorder, vector& postorder) { @@ -277,7 +277,7 @@ public: 下面给出用下表索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下表索引来分割) ### C++优化版本 -```C++ +```CPP class Solution { private: // 中序区间:[inorderBegin, inorderEnd),后序区间[postorderBegin, postorderEnd) @@ -325,7 +325,7 @@ public: 那么这个版本写出来依然要打日志进行调试,打日志的版本如下:(**该版本不要在leetcode上提交,容易超时**) -```C++ +```CPP class Solution { private: TreeNode* traversal (vector& inorder, int inorderBegin, int inorderEnd, vector& postorder, int postorderBegin, int postorderEnd) { @@ -419,7 +419,7 @@ public: 带日志的版本C++代码如下: (**带日志的版本仅用于调试,不要在leetcode上提交,会超时**) -```C++ +```CPP class Solution { private: TreeNode* traversal (vector& inorder, int inorderBegin, int inorderEnd, vector& preorder, int preorderBegin, int preorderEnd) { @@ -493,7 +493,7 @@ public: 105.从前序与中序遍历序列构造二叉树,最后版本,C++代码: -```C++ +```CPP class Solution { private: TreeNode* traversal (vector& inorder, int inorderBegin, int inorderEnd, vector& preorder, int preorderBegin, int preorderEnd) { @@ -818,4 +818,4 @@ var buildTree = function(preorder, inorder) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 8ec5f3dc..9e67a677 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -122,7 +122,7 @@ return root; * 递归整体代码如下: -```C++ +```CPP class Solution { private: TreeNode* traversal(vector& nums, int left, int right) { @@ -150,7 +150,7 @@ public: 模拟的就是不断分割的过程,C++代码如下:(我已经详细注释) -```C++ +```CPP class Solution { public: TreeNode* sortedArrayToBST(vector& nums) { @@ -388,4 +388,4 @@ var sortedArrayToBST = function (nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 324b3678..55d3c2e7 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -57,7 +57,7 @@ 在[104.二叉树的最大深度](https://mp.weixin.qq.com/s/jRaRcRerhEHepQbt-aKstw)中,如果真正求取二叉树的最大深度,代码应该写成如下:(前序遍历) -```C++ +```CPP class Solution { public: int result; @@ -91,7 +91,7 @@ public: 注意以上代码是为了把细节体现出来,简化一下代码如下: -```C++ +```CPP class Solution { public: int result; @@ -161,7 +161,7 @@ if (node == NULL) { 代码如下: -```C++ +```CPP int leftDepth = depth(node->left); // 左 if (leftDepth == -1) return -1; int rightDepth = depth(node->right); // 右 @@ -179,7 +179,7 @@ return result; 代码精简之后如下: -```C++ +```CPP int leftDepth = getDepth(node->left); if (leftDepth == -1) return -1; int rightDepth = getDepth(node->right); @@ -191,7 +191,7 @@ return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth); getDepth整体代码如下: -```C++ +```CPP int getDepth(TreeNode* node) { if (node == NULL) { return 0; @@ -206,7 +206,7 @@ int getDepth(TreeNode* node) { 最后本题整体递归代码如下: -```C++ +```CPP class Solution { public: // 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1 @@ -236,7 +236,7 @@ public: 代码如下: -```C++ +```CPP // cur节点的最大深度,就是cur的高度 int getDepth(TreeNode* cur) { stack st; @@ -267,7 +267,7 @@ int getDepth(TreeNode* cur) { 然后再用栈来模拟前序遍历,遍历每一个节点的时候,再去判断左右孩子的高度是否符合,代码如下: -```C++ +```CPP bool isBalanced(TreeNode* root) { stack st; if (root == NULL) return true; @@ -287,7 +287,7 @@ bool isBalanced(TreeNode* root) { 整体代码如下: -```C++ +```CPP class Solution { private: int getDepth(TreeNode* cur) { @@ -624,4 +624,4 @@ var isBalanced = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 2ef380ab..b4d0e32b 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -87,7 +87,7 @@ return result; 代码如下: -```C++ +```CPP int leftDepth = getDepth(node->left); // 左 int rightDepth = getDepth(node->right); // 右 // 中 @@ -106,7 +106,7 @@ return result; 遍历的顺序为后序(左右中),可以看出:**求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。** 整体递归代码如下: -```C++ +```CPP class Solution { public: int getDepth(TreeNode* node) { @@ -134,7 +134,7 @@ public: 精简之后代码如下: -```C++ +```CPP class Solution { public: int minDepth(TreeNode* root) { @@ -162,7 +162,7 @@ public: 代码如下:(详细注释) -```C++ +```CPP class Solution { public: @@ -413,4 +413,4 @@ var minDepth = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 474e17da..b59180ac 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -91,7 +91,7 @@ if (!cur->left && !cur->right) return false; // 遇到叶子节点而没有找 代码如下: -```C++ +```CPP if (cur->left) { // 左 (空节点不遍历) // 遇到叶子节点返回true,则直接返回true if (traversal(cur->left, count - cur->left->val)) return true; // 注意这里有回溯的逻辑 @@ -109,7 +109,7 @@ return false; 为了把回溯的过程体现出来,可以改为如下代码: -```C++ +```CPP if (cur->left) { // 左 count -= cur->left->val; // 递归,处理节点; if (traversal(cur->left, count)) return true; @@ -126,7 +126,7 @@ return false; 整体代码如下: -```C++ +```CPP class Solution { private: bool traversal(TreeNode* cur, int count) { @@ -156,7 +156,7 @@ public: 以上代码精简之后如下: -```C++ +```CPP class Solution { public: bool hasPathSum(TreeNode* root, int sum) { @@ -186,7 +186,7 @@ C++就我们用pair结构来存放这个栈里的元素。 如下代码是使用栈模拟的前序遍历,如下:(详细注释) -```C++ +```CPP class Solution { public: @@ -243,7 +243,7 @@ public: 为了尽可能的把细节体现出来,我写出如下代码(**这份代码并不简洁,但是逻辑非常清晰**) -```C++ +```CPP class Solution { private: vector> result; @@ -734,4 +734,4 @@ var pathSum = function(root, targetSum) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index 014eb3cd..e964e95d 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -82,7 +82,7 @@ dp[0][0]应该是1,空字符串s,可以删除0个元素,变成空字符串 初始化分析完毕,代码如下: -```C++ +```CPP vector> dp(s.size() + 1, vector(t.size() + 1)); for (int i = 0; i <= s.size(); i++) dp[i][0] = 1; for (int j = 1; j <= t.size(); j++) dp[0][j] = 0; // 其实这行代码可以和dp数组初始化的时候放在一起,但我为了凸显初始化的逻辑,所以还是加上了。 @@ -97,7 +97,7 @@ for (int j = 1; j <= t.size(); j++) dp[0][j] = 0; // 其实这行代码可以和 代码如下: -```C++ +```CPP for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { @@ -120,7 +120,7 @@ for (int i = 1; i <= s.size(); i++) { 动规五部曲分析完毕,代码如下: -```C++ +```CPP class Solution { public: int numDistinct(string s, string t) { @@ -250,4 +250,4 @@ const numDistinct = (s, t) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 6feb1ca9..f96351aa 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -52,7 +52,7 @@ struct Node { 图中cur节点为元素4,那么搭线的逻辑代码:(**注意注释中操作1和操作2和图中的对应关系**) -```C++ +```CPP if (cur->left) cur->left->next = cur->right; // 操作1 if (cur->right) { if (cur->next) cur->right->next = cur->next->left; // 操作2 @@ -63,7 +63,7 @@ if (cur->right) { 理解到这里,使用前序遍历,那么不难写出如下代码: -```C++ +```CPP class Solution { private: void traversal(Node* cur) { @@ -93,7 +93,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: Node* connect(Node* root) { @@ -177,5 +177,5 @@ const connect = root => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 8ad6c8c6..6d068c22 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -33,7 +33,7 @@ 这道题目最直观的想法,就是暴力,找最优间距了。 -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -59,7 +59,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -139,7 +139,7 @@ dp[5][1]就是最终结果。 以上分析完毕,C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -169,7 +169,7 @@ dp[i][1] = max(dp[i - 1][1], prices[i] + dp[i - 1][0]); 那么我们只需要记录 当前天的dp状态和前一天的dp状态就可以了,可以使用滚动数组来节省空间,代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -340,4 +340,4 @@ const maxProfit = prices => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 60d4591f..53737532 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -80,7 +80,7 @@ 对应C++代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -99,7 +99,7 @@ public: 动态规划将在下一个系列详细讲解,本题解先给出我的C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -239,4 +239,4 @@ var maxProfit = function(prices) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 8ed70063..7492ee04 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -74,7 +74,7 @@ 代码如下:(注意代码中的注释,标记了和121.买卖股票的最佳时机唯一不同的地方) -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -106,7 +106,7 @@ dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); 这里我依然给出滚动数组的版本,C++代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -231,4 +231,4 @@ const maxProfit = (prices) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index c7193bbc..0c92a5a2 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -127,7 +127,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 以上五部都分析完了,不难写出如下代码: -```C++ +```CPP // 版本一 class Solution { public: @@ -153,7 +153,7 @@ public: 当然,大家可以看到力扣官方题解里的一种优化空间写法,我这里给出对应的C++版本: -```C++ +```CPP // 版本二 class Solution { public: @@ -324,4 +324,4 @@ const maxProfit = prices => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index f8c93382..b37270e2 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -52,7 +52,7 @@ if (!cur->left && !cur->right) { // 遇到了叶子节点 这里vectorToInt函数就是把数组转成int,代码如下: -```C++ +```CPP int vectorToInt(const vector& vec) { int sum = 0; for (int i = 0; i < vec.size(); i++) { @@ -78,7 +78,7 @@ int vectorToInt(const vector& vec) { 代码如下: -```C++ +```CPP // 中 if (cur->left) { // 左 (空节点不遍历) path.push_back(cur->left->val); @@ -94,7 +94,7 @@ if (cur->right) { // 右 (空节点不遍历) 这里要注意回溯和递归要永远在一起,一个递归,对应一个回溯,是一对一的关系,有的同学写成如下代码: -```C++ +```CPP if (cur->left) { // 左 (空节点不遍历) path.push_back(cur->left->val); traversal(cur->left); // 递归 @@ -111,7 +111,7 @@ path.pop_back(); // 回溯 关键逻辑分析完了,整体C++代码如下: -```C++ +```CPP class Solution { private: int result; @@ -176,4 +176,4 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 43453409..6fe2758b 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -70,7 +70,7 @@ 代码如下: -```C++ +```CPP vector> result; vector path; // 放已经回文的子串 void backtracking (const string& s, int startIndex) { @@ -88,7 +88,7 @@ void backtracking (const string& s, int startIndex) { 所以终止条件代码如下: -```C++ +```CPP void backtracking (const string& s, int startIndex) { // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了 if (startIndex >= s.size()) { @@ -108,7 +108,7 @@ void backtracking (const string& s, int startIndex) { 代码如下: -```C++ +```CPP for (int i = startIndex; i < s.size(); i++) { if (isPalindrome(s, startIndex, i)) { // 是回文子串 // 获取[startIndex,i]在s中的子串 @@ -132,7 +132,7 @@ for (int i = startIndex; i < s.size(); i++) { 那么判断回文的C++代码如下: -```C++ +```CPP bool isPalindrome(const string& s, int start, int end) { for (int i = start, j = end; i < j; i++, j--) { if (s[i] != s[j]) { @@ -151,7 +151,7 @@ for (int i = startIndex; i < s.size(); i++) { 根据Carl给出的回溯算法模板: -```C++ +```CPP void backtracking(参数) { if (终止条件) { 存放结果; @@ -169,7 +169,7 @@ void backtracking(参数) { 不难写出如下代码: -```C++ +```CPP class Solution { private: vector> result; @@ -395,4 +395,4 @@ var partition = function(s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0132.分割回文串II.md b/problems/0132.分割回文串II.md index 856262d6..80784301 100644 --- a/problems/0132.分割回文串II.md +++ b/problems/0132.分割回文串II.md @@ -101,7 +101,7 @@ dp[i]: 范围是[0, i]的回文子串,最少分割次数是dp[i]。 代码如下: -```C++ +```CPP vector dp(s.size(), INT_MAX); dp[0] = 0; ``` @@ -109,7 +109,7 @@ dp[0] = 0; 其实也可以这样初始化,更具dp[i]的定义,dp[i]的最大值其实就是i,也就是把每个字符分割出来。 所以初始化代码也可以为: -```C++ +```CPP vector dp(s.size()); for (int i = 0; i < s.size(); i++) dp[i] = i; ``` @@ -122,7 +122,7 @@ j是在[0,i]之间,所以遍历i的for循环一定在外层,这里遍历j 代码如下: -```C++ +```CPP for (int i = 1; i < s.size(); i++) { if (isPalindromic[0][i]) { // 判断是不是回文子串 dp[i] = 0; @@ -149,7 +149,7 @@ for (int i = 1; i < s.size(); i++) { 代码如下: -```C++ +```CPP vector> isPalindromic(s.size(), vector(s.size(), false)); for (int i = s.size() - 1; i >= 0; i--) { for (int j = i; j < s.size(); j++) { @@ -168,7 +168,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 以上分析完毕,代码如下: -```C++ +```CPP class Solution { public: int minCut(string s) { @@ -252,5 +252,5 @@ class Solution: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 9b660ea0..e270c059 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -65,7 +65,7 @@ cost = [3,4,3] C++代码如下: -```C++ +```CPP class Solution { public: int canCompleteCircuit(vector& gas, vector& cost) { @@ -99,7 +99,7 @@ C++暴力解法在leetcode上提交也可以过。 C++代码如下: -```C++ +```CPP class Solution { public: int canCompleteCircuit(vector& gas, vector& cost) { @@ -160,7 +160,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i C++代码如下: -```C++ +```CPP class Solution { public: int canCompleteCircuit(vector& gas, vector& cost) { @@ -288,4 +288,4 @@ var canCompleteCircuit = function(gas, cost) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index 2d3fca84..3865e14b 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -47,7 +47,7 @@ 代码如下: -```C++ +```CPP // 从前向后 for (int i = 1; i < ratings.size(); i++) { if (ratings[i] > ratings[i - 1]) candyVec[i] = candyVec[i - 1] + 1; @@ -80,7 +80,7 @@ for (int i = 1; i < ratings.size(); i++) { 所以该过程代码如下: -```C++ +```CPP // 从后向前 for (int i = ratings.size() - 2; i >= 0; i--) { if (ratings[i] > ratings[i + 1] ) { @@ -90,7 +90,7 @@ for (int i = ratings.size() - 2; i >= 0; i--) { ``` 整体代码如下: -```C++ +```CPP class Solution { public: int candy(vector& ratings) { @@ -242,4 +242,4 @@ var candy = function(ratings) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 4ba08aa9..0c19a614 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -45,7 +45,7 @@ 那么这里我也给出回溯法C++代码: -```C++ +```CPP class Solution { private: bool backtracking (const string& s, const unordered_set& wordSet, int startIndex) { @@ -86,7 +86,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { private: bool backtracking (const string& s, @@ -190,7 +190,7 @@ dp[s.size()]就是最终结果。 动规五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: bool wordBreak(string s, vector& wordDict) { @@ -319,4 +319,4 @@ const wordBreak = (s, wordDict) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md index 0871202d..78bcfd43 100644 --- a/problems/0141.环形链表.md +++ b/problems/0141.环形链表.md @@ -45,7 +45,7 @@ fast和slow各自再走一步, fast和slow就相遇了 C++代码如下 -```C++ +```CPP class Solution { public: bool hasCycle(ListNode *head) { @@ -120,5 +120,5 @@ class Solution: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index 9deb1e0c..91e14e27 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -109,7 +109,7 @@ fast指针走过的节点数:` x + y + n (y + z)`,n为fast指针在环内走 代码如下: -```C++ +```CPP /** * Definition for singly-linked list. * struct ListNode { @@ -301,4 +301,4 @@ var detectCycle = function(head) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index dc0095d3..62232051 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -24,7 +24,7 @@ 代码如下: -```C++ +```CPP class Solution { public: void reorderList(ListNode* head) { @@ -63,7 +63,7 @@ public: 把链表放进双向队列,然后通过双向队列一前一后弹出数据,来构造新的链表。这种方法比操作数组容易一些,不用双指针模拟一前一后了 -```C++ +```CPP class Solution { public: void reorderList(ListNode* head) { @@ -108,7 +108,7 @@ public: 代码如下: -```C++ +```CPP class Solution { private: // 反转链表 @@ -233,5 +233,5 @@ JavaScript: * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index aceb91b4..2b294337 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -80,7 +80,7 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ C++代码如下: -```C++ +```CPP class Solution { public: int evalRPN(vector& tokens) { @@ -241,4 +241,4 @@ def evalRPN(tokens) -> int: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 32d9deef..3f4d00b2 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -61,7 +61,7 @@ https://leetcode-cn.com/problems/reverse-words-in-a-string/ 思路很明确了,我们说一说代码的实现细节,就拿移除多余空格来说,一些同学会上来写如下代码: -```C++ +```CPP void removeExtraSpaces(string& s) { for (int i = s.size() - 1; i > 0; i--) { if (s[i] == s[i - 1] && s[i] == ' ') { @@ -93,7 +93,7 @@ erase操作上面还套了一个for循环,那么以上代码移除冗余空格 那么使用双指针来移除冗余空格代码如下: fastIndex走的快,slowIndex走的慢,最后slowIndex就标记着移除多余空格后新字符串的长度。 -```C++ +```CPP void removeExtraSpaces(string& s) { int slowIndex = 0, fastIndex = 0; // 定义快指针,慢指针 // 去掉字符串前面的空格 @@ -141,7 +141,7 @@ void reverse(string& s, int start, int end) { 本题C++整体代码 -```C++ +```CPP // 版本一 class Solution { public: @@ -474,4 +474,4 @@ function reverse(strArr, start, end) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0160.相交链表.md b/problems/0160.相交链表.md index d26f66fd..7ac1848b 100644 --- a/problems/0160.相交链表.md +++ b/problems/0160.相交链表.md @@ -1,2 +1,3 @@ 同:[链表:链表相交](./面试题02.07.链表相交.md) +
diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 46c6f7f0..9fe7a919 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -84,7 +84,7 @@ vector> dp(prices.size(), vector(2 * k + 1, 0)); 同理可以类比剩下的状态,代码如下: -```C++ +```CPP for (int j = 0; j < 2 * k - 1; j += 2) { dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]); @@ -117,7 +117,7 @@ for (int j = 0; j < 2 * k - 1; j += 2) { 代码如下: -```C++ +```CPP for (int j = 1; j < 2 * k; j += 2) { dp[0][j] = -prices[0]; } @@ -139,7 +139,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int maxProfit(int k, vector& prices) { @@ -287,4 +287,4 @@ const maxProfit = (k,prices) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index 9f565c1d..be46bf4c 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -69,7 +69,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: void rotate(vector& nums, int k) { @@ -137,6 +137,6 @@ class Solution: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index 5b5863a4..93b56dae 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -59,7 +59,7 @@ 代码如下: -```C++ +```CPP vector dp(nums.size()); dp[0] = nums[0]; dp[1] = max(nums[0], nums[1]); @@ -70,7 +70,7 @@ dp[1] = max(nums[0], nums[1]); dp[i] 是根据dp[i - 2] 和 dp[i - 1] 推导出来的,那么一定是从前到后遍历! 代码如下: -```C++ +```CPP for (int i = 2; i < nums.size(); i++) { dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); } @@ -86,7 +86,7 @@ for (int i = 2; i < nums.size(); i++) { 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int rob(vector& nums) { @@ -198,4 +198,4 @@ const rob = nums => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index d1bccd64..1c630b6a 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -46,7 +46,7 @@ https://leetcode-cn.com/problems/happy-number/ C++代码如下: -```C++ +```CPP class Solution { public: // 取数值各个位上的单数之和 @@ -193,4 +193,4 @@ var isHappy = function(n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index ea1d705a..a2c6e90d 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -89,7 +89,7 @@ https://leetcode-cn.com/problems/remove-linked-list-elements/ **直接使用原来的链表来进行移除节点操作:** -```C++ +```CPP class Solution { public: ListNode* removeElements(ListNode* head, int val) { @@ -118,7 +118,7 @@ public: **设置一个虚拟头结点在进行移除节点操作:** -```C++ +```CPP class Solution { public: ListNode* removeElements(ListNode* head, int val) { @@ -311,4 +311,4 @@ var removeElements = function(head, val) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index 7ffa3fbf..d5077e03 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -39,7 +39,7 @@ C++代码 如下: -```C++ +```CPP class Solution { public: bool isIsomorphic(string s, string t) { @@ -108,5 +108,5 @@ class Solution { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 27a2e903..8bb359bd 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -48,7 +48,7 @@ https://leetcode-cn.com/problems/reverse-linked-list/ # C++代码 ## 双指针法 -```C++ +```CPP class Solution { public: ListNode* reverseList(ListNode* head) { @@ -74,7 +74,7 @@ public: 关键是初始化的地方,可能有的同学会不理解, 可以看到双指针法中初始化 cur = head,pre = NULL,在递归法中可以从如下代码看出初始化的逻辑也是一样的,只不过写法变了。 具体可以看代码(已经详细注释),**双指针法写出来之后,理解如下递归写法就不难了,代码逻辑都是一样的。** -```C++ +```CPP class Solution { public: ListNode* reverse(ListNode* pre,ListNode* cur){ @@ -338,4 +338,4 @@ fun reverseList(head: ListNode?): ListNode? { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 42687514..3219d16f 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -26,7 +26,7 @@ 代码如下: -```C++ +```CPP class Solution { public: int minSubArrayLen(int s, vector& nums) { @@ -86,7 +86,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { public: int minSubArrayLen(int s, vector& nums) { @@ -216,8 +216,31 @@ var minSubArrayLen = function(target, nums) { }; ``` +Swift: + +```swift +func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { + var result = Int.max + var sum = 0 + var starIndex = 0 + for endIndex in 0..= target { + result = min(result, endIndex - starIndex + 1) + sum -= nums[starIndex] + starIndex += 1 + } + } + + return result == Int.max ? 0 : result +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index 12951117..e828d17a 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -59,7 +59,7 @@ 代码如下: -```C++ +```CPP // 注意注释中的情况二情况三,以及把198.打家劫舍的代码抽离出来了 class Solution { public: @@ -174,4 +174,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 15220464..c9cfa973 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -94,7 +94,7 @@ void backtracking(int targetSum, int k, int sum, int startIndex) 所以 终止代码如下: -```C++ +```CPP if (path.size() == k) { if (sum == targetSum) result.push_back(path); return; // 如果path.size() == k 但sum != targetSum 直接返回 @@ -112,7 +112,7 @@ if (path.size() == k) { 代码如下: -```C++ +```CPP for (int i = startIndex; i <= 9; i++) { sum += i; path.push_back(i); @@ -126,7 +126,7 @@ for (int i = startIndex; i <= 9; i++) { 参照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中的模板,不难写出如下C++代码: -```C++ +```CPP class Solution { private: vector> result; // 存放结果集 @@ -398,4 +398,4 @@ var combinationSum3 = function(k, n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index c9319372..0d3a818e 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -77,7 +77,7 @@ return treeNum; 所以整体C++代码如下: -```C++ +```CPP // 版本一 class Solution { private: @@ -96,7 +96,7 @@ public: ``` 代码精简之后C++代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -119,7 +119,7 @@ public: 那么只要模板少做改动,加一个变量result,统计节点数量就可以了 -```C++ +```CPP class Solution { public: int countNodes(TreeNode* root) { @@ -163,7 +163,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { public: int countNodes(TreeNode* root) { @@ -435,4 +435,4 @@ var countNodes = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index b3e851a1..b327c17b 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -65,7 +65,7 @@ queue.empty(); 详细如代码注释所示: -```C++ +```CPP class MyStack { public: queue que1; @@ -118,7 +118,7 @@ public: C++优化代码 -```C++ +```CPP class MyStack { public: queue que; @@ -460,4 +460,4 @@ MyStack.prototype.empty = function() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 44f0d3b4..6eb6f301 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -89,7 +89,7 @@ invertTree(root->right); 基于这递归三步法,代码基本写完,C++代码如下: -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -111,7 +111,7 @@ public: C++代码迭代法(前序遍历) -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -136,7 +136,7 @@ public: C++代码如下迭代法(前序遍历) -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -168,7 +168,7 @@ public: 也就是层序遍历,层数遍历也是可以翻转这棵树的,因为层序遍历也可以把每个节点的左右孩子都翻转一遍,代码如下: -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -196,7 +196,7 @@ public: 如果非要使用递归中序的方式写,也可以,如下代码就可以避免节点左右孩子翻转两次的情况: -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -215,7 +215,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -478,4 +478,4 @@ var invertTree = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 32476d6f..0df82d35 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -67,7 +67,7 @@ queue.empty(); C++代码如下: -```C++ +```CPP class MyQueue { public: stack stIn; @@ -446,4 +446,4 @@ MyQueue.prototype.empty = function() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index 945d2ef4..6a24b1d0 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -30,7 +30,7 @@ 代码也比较简单。如下: -```C++ +```CPP class Solution { public: bool isPalindrome(ListNode* head) { @@ -51,7 +51,7 @@ public: 上面代码可以在优化,就是先求出链表长度,然后给定vector的初始长度,这样避免vector每次添加节点重新开辟空间 -```C++ +```CPP class Solution { public: bool isPalindrome(ListNode* head) { @@ -95,7 +95,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: bool isPalindrome(ListNode* head) { @@ -166,5 +166,5 @@ public: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index dffc89e6..fe875067 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -93,7 +93,7 @@ if (cur == NULL) return cur; 代码如下: -```C++ +```CPP if (cur->val > p->val && cur->val > q->val) { TreeNode* left = traversal(cur->left, p, q); if (left != NULL) { @@ -147,7 +147,7 @@ return cur; 那么整体递归代码如下: -```C++ +```CPP class Solution { private: TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q) { @@ -177,7 +177,7 @@ public: 精简后代码如下: -```C++ +```CPP class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { @@ -198,7 +198,7 @@ public: 迭代代码如下: -```C++ +```CPP class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { @@ -360,4 +360,4 @@ var lowestCommonAncestor = function(root, p, q) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 41e12df4..0885e20f 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -150,7 +150,7 @@ TreeNode* right = lowestCommonAncestor(root->right, p, q); 代码如下: -```C++ +```CPP if (left == NULL && right != NULL) return right; else if (left != NULL && right == NULL) return left; else { // (left == NULL && right == NULL) @@ -167,7 +167,7 @@ else { // (left == NULL && right == NULL) 整体代码如下: -```C++ +```CPP class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { @@ -188,7 +188,7 @@ public: 稍加精简,代码如下: -```C++ +```CPP class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { @@ -342,4 +342,4 @@ var lowestCommonAncestor = function(root, p, q) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index dedc3247..3c12a985 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -108,7 +108,7 @@ public: 基于刚刚说过的单调队列pop和push的规则,代码不难实现,如下: -```C++ +```CPP class MyQueue { //单调队列(从大到小) public: deque que; // 使用deque来实现单调队列 @@ -140,7 +140,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { private: class MyQueue { //单调队列(从大到小) @@ -425,4 +425,4 @@ var maxSlidingWindow = function (nums, k) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 93bba44c..b215a88a 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -61,7 +61,7 @@ https://leetcode-cn.com/problems/valid-anagram/ C++ 代码如下: -```C++ +```CPP class Solution { public: bool isAnagram(string s, string t) { @@ -209,4 +209,4 @@ var isAnagram = function(s, t) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index bb0513de..84315507 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -77,7 +77,7 @@ if (cur->left == NULL && cur->right == NULL) { 这里我们先使用vector结构的path容器来记录路径,那么终止处理逻辑如下: -```C++ +```CPP if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点 string sPath; for (int i = 0; i < path.size() - 1; i++) { // 将path里记录的路径转为string格式 @@ -113,7 +113,7 @@ if (cur->right) { 那么回溯要怎么回溯呢,一些同学会这么写,如下: -```C++ +```CPP if (cur->left) { traversal(cur->left, path, result); } @@ -129,7 +129,7 @@ path.pop_back(); 那么代码应该这么写: -```C++ +```CPP if (cur->left) { traversal(cur->left, path, result); path.pop_back(); // 回溯 @@ -142,7 +142,7 @@ if (cur->right) { 那么本题整体代码如下: -```C++ +```CPP class Solution { private: @@ -183,7 +183,7 @@ public: 那么如上代码可以精简成如下代码: -```C++ +```CPP class Solution { private: @@ -217,20 +217,20 @@ public: 为了把这份精简代码的回溯过程展现出来,大家可以试一试把: -```C++ +```CPP if (cur->left) traversal(cur->left, path + "->", result); // 左 回溯就隐藏在这里 ``` 改成如下代码: -```C++ +```CPP path += "->"; traversal(cur->left, path, result); // 左 ``` 即: -```C++ +```CPP if (cur->left) { path += "->"; traversal(cur->left, path, result); // 左 @@ -245,7 +245,7 @@ if (cur->right) { 如果想把回溯加上,就要 在上面代码的基础上,加上回溯,就可以AC了。 -```C++ +```CPP if (cur->left) { path += "->"; traversal(cur->left, path, result); // 左 @@ -276,7 +276,7 @@ if (cur->right) { C++代码如下: -```C++ +```CPP class Solution { public: vector binaryTreePaths(TreeNode* root) { @@ -447,4 +447,4 @@ var binaryTreePaths = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index d0922de1..e51f0a99 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -76,7 +76,7 @@ dp[0]表示 和为0的完全平方数的最小数量,那么dp[0]一定是0。 我这里先给出外层遍历背包,里层遍历物品的代码: -```C++ +```CPP vector dp(n + 1, INT_MAX); dp[0] = 0; for (int i = 0; i <= n; i++) { // 遍历背包 @@ -106,7 +106,7 @@ dp[5] = min(dp[4] + 1, dp[1] + 1) = 2 以上动规五部曲分析完毕C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -125,7 +125,7 @@ public: 同样我在给出先遍历物品,在遍历背包的代码,一样的可以AC的。 -```C++ +```CPP // 版本二 class Solution { public: @@ -286,10 +286,38 @@ func min(a, b int) int { } ``` +Javascript: +```Javascript +// 先遍历物品,再遍历背包 +var numSquares1 = function(n) { + let dp = new Array(n + 1).fill(Infinity) + dp[0] = 0 + for(let i = 0; i <= n; i++) { + let val = i * i + for(let j = val; j <= n; j++) { + dp[j] = Math.min(dp[j], dp[j - val] + 1) + } + } + return dp[n] +}; +// 先遍历背包,再遍历物品 +var numSquares2 = function(n) { + let dp = new Array(n + 1).fill(Infinity) + dp[0] = 0 + + for(let i = 1; i <= n; i++) { + for(let j = 1; j * j <= i; j++) { + dp[i] = Math.min(dp[i - j * j] + 1, dp[i]) + } + } + + return dp[n] +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 8db42a0a..af7142fa 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -42,7 +42,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: void moveZeroes(vector& nums) { @@ -100,5 +100,5 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 9a7c543b..57edd13e 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -60,7 +60,7 @@ dp[i] 是有0到i-1各个位置的最长升序子序列 推导而来,那么遍 j其实就是0到i-1,遍历i的循环里外层,遍历j则在内层,代码如下: -```C++ +```CPP for (int i = 1; i < nums.size(); i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1); @@ -80,7 +80,7 @@ for (int i = 1; i < nums.size(); i++) { 以上五部分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int lengthOfLIS(vector& nums) { @@ -198,4 +198,4 @@ const lengthOfLIS = (nums) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index e28e8369..3b1b6500 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -95,7 +95,7 @@ p[i][3] = dp[i - 1][2]; 综上分析,递推代码如下: -```C++ +```CPP dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i]; dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); dp[i][2] = dp[i - 1][0] + prices[i]; @@ -129,7 +129,7 @@ dp[i][3] = dp[i - 1][2]; 代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -236,4 +236,4 @@ const maxProfit = (prices) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 302ae789..758bd5b8 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -112,7 +112,7 @@ dp[amount]为最终结果。 ## C++代码 以上分析完毕,C++ 代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -134,7 +134,7 @@ public: 对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了 -```C++ +```CPP // 版本二 class Solution { public: @@ -330,4 +330,4 @@ const coinChange = (coins, amount) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 3c7e34d8..8a28899b 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -178,7 +178,7 @@ if (result.size() == ticketNum + 1) { 遍历过程如下: -```C++ +```CPP for (pair& target : targets[result[result.size() - 1]]) { if (target.second > 0 ) { // 记录到达机场是否飞过了 result.push_back(target.first); @@ -194,7 +194,7 @@ for (pair& target : targets[result[result.size() - 1]]) { 分析完毕,此时完整C++代码如下: -```C++ +```CPP class Solution { private: // unordered_map<出发机场, map<到达机场, 航班次数>> targets @@ -450,4 +450,4 @@ var findItinerary = function(tickets) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 780e4aef..44e60fb3 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -35,7 +35,7 @@ 代码如下: -```C++ +```CPP class Solution { public: int rob(TreeNode* root) { @@ -65,7 +65,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: unordered_map umap; // 记录计算过的结果 @@ -103,7 +103,7 @@ public: 参数为当前节点,代码如下: -```C++ +```CPP vector robTree(TreeNode* cur) { ``` @@ -138,7 +138,7 @@ if (cur == NULL) return vector{0, 0}; 代码如下: -```C++ +```CPP // 下标0:不偷,下标1:偷 vector left = robTree(cur->left); // 左 vector right = robTree(cur->right); // 右 @@ -156,7 +156,7 @@ vector right = robTree(cur->right); // 右 代码如下: -```C++ +```CPP vector left = robTree(cur->left); // 左 vector right = robTree(cur->right); // 右 @@ -179,7 +179,7 @@ return {val2, val1}; 递归三部曲与动规五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int rob(TreeNode* root) { @@ -402,4 +402,4 @@ const rob = root => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index ec1ed91a..a9c55a85 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -105,7 +105,7 @@ for (int i = 3; i <= n ; i++) { 以上动规五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int integerBreak(int n) { @@ -132,7 +132,7 @@ public: 给出我的C++代码如下: -```C++ +```CPP class Solution { public: int integerBreak(int n) { @@ -158,7 +158,7 @@ public: 其实这道题目的递推公式并不好想,而且初始化的地方也很有讲究,我在写本题的时候一开始写的代码是这样的: -```C++ +```CPP class Solution { public: int integerBreak(int n) { @@ -248,4 +248,4 @@ var integerBreak = function(n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index aeb13a30..4f96f839 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -74,7 +74,7 @@ https://leetcode-cn.com/problems/reverse-string/ 不难写出如下C++代码: -```C++ +```CPP void reverseString(vector& s) { for (int i = 0, j = s.size() - 1; i < s.size()/2; i++, j--) { swap(s[i],s[j]); @@ -90,7 +90,7 @@ swap可以有两种实现。 一种就是常见的交换数值: -```C++ +```CPP int tmp = s[i]; s[i] = s[j]; s[j] = tmp; @@ -99,7 +99,7 @@ s[j] = tmp; 一种就是通过位运算: -```C++ +```CPP s[i] ^= s[j]; s[j] ^= s[i]; s[i] ^= s[j]; @@ -120,7 +120,7 @@ s[i] ^= s[j]; C++代码如下: -```C++ +```CPP class Solution { public: void reverseString(vector& s) { @@ -219,4 +219,4 @@ var reverseString = function(s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 161df1ad..680de3bd 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -76,7 +76,7 @@ https://leetcode-cn.com/problems/top-k-frequent-elements/ 我们来看一下C++代码: -```C++ +```CPP // 时间复杂度:O(nlogk) // 空间复杂度:O(n) class Solution { @@ -291,4 +291,4 @@ PriorityQueue.prototype.compare = function(index1, index2) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 5dc18478..29c1c144 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -54,7 +54,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 C++代码如下: -```C++ +```CPP class Solution { public: vector intersection(vector& nums1, vector& nums2) { @@ -188,4 +188,4 @@ var intersection = function(nums1, nums2) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index f64e0043..0ccc405a 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -70,7 +70,7 @@ C++代码如下(和上图是对应的逻辑): -```C++ +```CPP class Solution { public: int wiggleMaxLength(vector& nums) { @@ -192,4 +192,4 @@ var wiggleMaxLength = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index 2ee009b3..3f735727 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -107,7 +107,7 @@ dp[i](考虑nums[j])可以由 dp[i - nums[j]](不考虑nums[j]) 推导 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int combinationSum4(vector& nums, int target) { @@ -226,4 +226,4 @@ const combinationSum4 = (nums, target) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index a5315b0e..f2c1685a 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -40,7 +40,7 @@ canConstruct("aa", "aab") -> true 那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下: -```C++ +```CPP // 时间复杂度: O(n^2) // 空间复杂度:O(1) class Solution { @@ -79,7 +79,7 @@ public: 代码如下: -```C++ +```CPP // 时间复杂度: O(n) // 空间复杂度:O(1) class Solution { @@ -256,4 +256,4 @@ var canConstruct = function(ransomNote, magazine) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index 09ee2c4d..bcf3462b 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -107,7 +107,7 @@ dp[i][j]表示以下标i-1为结尾的字符串s和以下标j-1为结尾的字 动规五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: bool isSubsequence(string s, string t) { @@ -212,4 +212,4 @@ const isSubsequence = (s, t) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 2a76a461..84ec2310 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -7,7 +7,7 @@

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

-## 404.左叶子之和 +# 404.左叶子之和 题目地址:https://leetcode-cn.com/problems/sum-of-left-leaves/ @@ -17,7 +17,7 @@ ![404.左叶子之和1](https://img-blog.csdnimg.cn/20210204151927654.png) -## 思路 +# 思路 **首先要注意是判断左叶子,不是二叉树左侧节点,所以不要上来想着层序遍历。** @@ -65,7 +65,7 @@ if (root == NULL) return 0; 代码如下: -```C++ +```CPP int leftValue = sumOfLeftLeaves(root->left); // 左 int rightValue = sumOfLeftLeaves(root->right); // 右 // 中 @@ -81,7 +81,7 @@ return sum; 整体递归代码如下: -```C++ +```CPP class Solution { public: int sumOfLeftLeaves(TreeNode* root) { @@ -102,7 +102,7 @@ public: 以上代码精简之后如下: -```C++ +```CPP class Solution { public: int sumOfLeftLeaves(TreeNode* root) { @@ -119,11 +119,11 @@ public: ## 迭代法 -本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://mp.weixin.qq.com/s/c_zCrGHIVlBjUH_hJtghCg)和[二叉树:前中后序迭代方式的写法就不能统一一下么?](https://mp.weixin.qq.com/s/WKg0Ty1_3SZkztpHubZPRg)中的写法,可以写出一个前序遍历的迭代法。 +本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://mp.weixin.qq.com/s/OH7aCVJ5-Gi32PkNCoZk4A)和[二叉树:迭代法统一写法](https://mp.weixin.qq.com/s/ATQMPCpBlaAgrqdLDMVPZA)中的写法,可以写出一个前序遍历的迭代法。 判断条件都是一样的,代码如下: -```C++ +```CPP class Solution { public: @@ -146,7 +146,7 @@ public: }; ``` -## 总结 +# 总结 这道题目要求左叶子之和,其实是比较绕的,因为不能判断本节点是不是左叶子节点。 @@ -157,9 +157,9 @@ public: 希望通过这道题目,可以扩展大家对二叉树的解题思路。 -## 其他语言版本 +# 其他语言版本 -Java: +## Java **递归** @@ -204,7 +204,7 @@ class Solution { -Python: +## Python **递归** ```python @@ -250,19 +250,11 @@ class Solution: return res ``` -Go: +## Go -> 递归法 +**递归法** ```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ func sumOfLeftLeaves(root *TreeNode) int { var res int findLeft(root,&res) @@ -282,17 +274,9 @@ func findLeft(root *TreeNode,res *int){ } ``` -> 迭代法 +**迭代法** ```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ func sumOfLeftLeaves(root *TreeNode) int { var res int queue:=list.New() @@ -317,8 +301,10 @@ func sumOfLeftLeaves(root *TreeNode) int { ``` -JavaScript: -递归版本 +## JavaScript + +**递归法** + ```javascript var sumOfLeftLeaves = function(root) { //采用后序遍历 递归遍历 @@ -340,8 +326,9 @@ var sumOfLeftLeaves = function(root) { } return nodesSum(root); }; -``` -迭代版本 +``` + +**迭代法** ```javascript var sumOfLeftLeaves = function(root) { //采用层序遍历 @@ -371,4 +358,4 @@ var sumOfLeftLeaves = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index 699f67d0..e9b9a738 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -99,7 +99,7 @@ C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -127,7 +127,7 @@ public: 改成链表之后,C++代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -261,4 +261,4 @@ var reconstructQueue = function(people) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 75c665cd..0f1f094a 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -106,7 +106,7 @@ 代码如下: -```C++ +```CPP // 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200 // 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了 vector dp(10001, 0); @@ -118,7 +118,7 @@ vector dp(10001, 0); 代码如下: -```C++ +```CPP // 开始 01背包 for(int i = 0; i < nums.size(); i++) { for(int j = target; j >= nums[i]; j--) { // 每一个元素一定是不可重复放入,所以从大到小遍历 @@ -141,7 +141,7 @@ dp[i]的数值一定是小于等于i的。 综上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: bool canPartition(vector& nums) { @@ -266,4 +266,4 @@ var canPartition = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index b86f6714..70d0ff3a 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -126,7 +126,7 @@ public: 把[452.用最少数量的箭引爆气球](https://mp.weixin.qq.com/s/HxVAJ6INMfNKiGwI88-RFw)代码稍做修改,就可以AC本题。 -```C++ +```CPP class Solution { public: // 按照区间右边界排序 @@ -152,7 +152,7 @@ public: ``` 这里按照 左区间遍历,或者按照右边界遍历,都可以AC,具体原因我还没有仔细看,后面有空再补充。 -```C++ +```CPP class Solution { public: // 按照区间左边界排序 @@ -300,4 +300,4 @@ var eraseOverlapIntervals = function(intervals) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index e6ce469e..96c49468 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -78,7 +78,7 @@ if (root == nullptr) return root; 代码如下: -```C++ +```CPP if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 @@ -111,7 +111,7 @@ return root; **整体代码如下:(注释中:情况1,2,3,4,5和上面分析严格对应)** -```C++ +```CPP class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { @@ -156,7 +156,7 @@ public: 代码如下:(关键部分已经注释) -```C++ +```CPP class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { @@ -186,7 +186,7 @@ public: 代码如下: -```C++ +```CPP class Solution { private: // 将目标节点(删除节点)的左子树放到 目标节点的右子树的最左面节点的左孩子位置上 @@ -456,4 +456,4 @@ var deleteNode = function (root, key) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index a4db9e88..589105b2 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -84,7 +84,7 @@ C++代码如下: -```C++ +```CPP class Solution { private: static bool cmp(const vector& a, const vector& b) { @@ -223,4 +223,4 @@ var findMinArrowShots = function(points) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 0621ab5b..eafee43e 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -53,7 +53,7 @@ D = [ 0, 2] C++代码: -```C++ +```CPP class Solution { public: int fourSumCount(vector& A, vector& B, vector& C, vector& D) { @@ -234,4 +234,4 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 6b121e36..19268942 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -59,7 +59,7 @@ C++代码整体如下: -```C++ +```CPP // 时间复杂度:O(nlogn) // 空间复杂度:O(1) class Solution { @@ -88,7 +88,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: int findContentChildren(vector& g, vector& s) { @@ -188,4 +188,4 @@ var findContentChildren = function(g, s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 25b52e86..3b735942 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -70,7 +70,7 @@ next[len - 1] = 7,next[len - 1] + 1 = 8,8就是此时字符串asdfasdfasdf C++代码如下:(这里使用了前缀表统一减一的实现方式) -```C++ +```CPP class Solution { public: void getNext (int* next, const string& s){ @@ -104,7 +104,7 @@ public: 前缀表(不减一)的C++代码实现 -```C++ +```CPP class Solution { public: void getNext (int* next, const string& s){ @@ -369,4 +369,4 @@ var repeatedSubstringPattern = function (s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md index 40378854..15255d3e 100644 --- a/problems/0463.岛屿的周长.md +++ b/problems/0463.岛屿的周长.md @@ -16,7 +16,7 @@ https://leetcode-cn.com/problems/island-perimeter/ C++代码如下:(详细注释) -```C++ +```CPP class Solution { public: int direction[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; @@ -56,7 +56,7 @@ result = 岛屿数量 * 4 - cover * 2; C++代码如下:(详细注释) -```C++ +```CPP class Solution { public: int islandPerimeter(vector>& grid) { @@ -95,4 +95,4 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 6b73c080..90d1227a 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -95,7 +95,7 @@ dp[i][j] 就可以是 dp[i - zeroNum][j - oneNum] + 1。 那么本题也是,物品就是strs里的字符串,背包容量就是题目描述中的m和n。 代码如下: -```C++ +```CPP for (string str : strs) { // 遍历物品 int oneNum = 0, zeroNum = 0; for (char c : str) { @@ -126,7 +126,7 @@ for (string str : strs) { // 遍历物品 以上动规五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int findMaxForm(vector& strs, int m, int n) { @@ -279,4 +279,4 @@ const findMaxForm = (strs, m, n) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 103b8b10..ed712f34 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -105,7 +105,7 @@ for (int i = startIndex; i < nums.size(); i++) { 最后整体C++代码如下: -```C++ +```CPP // 版本一 class Solution { private: @@ -150,7 +150,7 @@ public: 那么优化后的代码如下: -```C++ +```CPP // 版本二 class Solution { private: @@ -320,4 +320,4 @@ var findSubsequences = function(nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index b7da0252..77b3c76d 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -67,7 +67,7 @@ target是固定的,sum是固定的,left就可以求出来。 我也把代码给出来吧,大家可以了解一下,回溯的解法,以下是本题转变为组合总和问题的回溯法代码: -```C++ +```CPP class Solution { private: vector> result; @@ -124,7 +124,7 @@ x = (S + sum) / 2 这么担心就对了,例如sum 是5,S是2的话其实就是无解的,所以: -```C++ +```CPP if ((S + sum) % 2 == 1) return 0; // 此时没有方案 ``` @@ -194,7 +194,7 @@ dp数组状态变化如下: C++代码如下: -```C++ +```CPP class Solution { public: int findTargetSumWays(vector& nums, int S) { @@ -342,4 +342,4 @@ const findTargetSumWays = (nums, target) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 566a09b1..589ee8ad 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -64,7 +64,7 @@ C++中,当我们要使用集合来解决哈希问题的时候,优先使用un 那么预处理代码如下: -```C++ +```CPP unordered_map umap; // key:下表元素,value:下表 for (int i = 0; i < nums1.size(); i++) { umap[nums1[i]] = i; @@ -100,7 +100,7 @@ for (int i = 0; i < nums1.size(); i++) { 代码如下: -```C++ +```CPP while (!st.empty() && nums2[i] > nums2[st.top()]) { if (umap.count(nums2[st.top()]) > 0) { // 看map里是否存在这个元素 int index = umap[nums2[st.top()]]; // 根据map找到nums2[st.top()] 在 nums1中的下表 @@ -114,7 +114,7 @@ st.push(i); 以上分析完毕,C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -152,7 +152,7 @@ public: 针对版本一,进行代码精简后,代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -238,3 +238,4 @@ class Solution: return result ``` +
diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index ac7ff603..f29fd08a 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -53,7 +53,7 @@ 这里采用前序遍历,代码如下: -```C++ +```CPP // map key:元素,value:出现频率 void searchBST(TreeNode* cur, unordered_map& map) { // 前序遍历 if (cur == NULL) return ; @@ -87,7 +87,7 @@ sort(vec.begin(), vec.end(), cmp); // 给频率排个序 代码如下: -```C++ +```CPP result.push_back(vec[0].first); for (int i = 1; i < vec.size(); i++) { // 取最高的放到result数组中 @@ -100,7 +100,7 @@ return result; 整体C++代码如下: -```C++ +```CPP class Solution { private: @@ -145,7 +145,7 @@ public: 中序遍历代码如下: -```C++ +```CPP void searchBST(TreeNode* cur) { if (cur == NULL) return ; searchBST(cur->left); // 左 @@ -217,7 +217,7 @@ if (count > maxCount) { // 如果计数大于最大值 关键代码都讲完了,完整代码如下:(**只需要遍历一遍二叉搜索树,就求出了众数的集合**) -```C++ +```CPP class Solution { private: int maxCount; // 最大频率 @@ -279,7 +279,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: vector findMode(TreeNode* root) { @@ -690,4 +690,4 @@ var findMode = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 6e351f27..9f38d80d 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -32,7 +32,7 @@ 代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -69,7 +69,7 @@ resize倒是不费时间,是O(1)的操作,但扩充nums数组相当于多了 代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -95,23 +95,29 @@ public: ## 其他语言版本 Java: -```java +```Java class Solution { public int[] nextGreaterElements(int[] nums) { - int[] res = new int[nums.length]; - Arrays.fill(res, -1); - Stack temp = new Stack<>(); - for (int i = 1; i < nums.length * 2; i++) { - while (!temp.isEmpty() && nums[temp.peek()] < nums[i % nums.length]) { - res[temp.pop()] = nums[i % nums.length]; - } - temp.add(i % nums.length); + //边界判断 + if(nums == null || nums.length <= 1) { + return new int[]{-1}; } - return res; + int size = nums.length; + int[] result = new int[size];//存放结果 + Arrays.fill(result,-1);//默认全部初始化为-1 + Stack st= new Stack<>();//栈中存放的是nums中的元素下标 + for(int i = 0; i < 2*size; i++) { + while(!st.empty() && nums[i % size] > nums[st.peek()]) { + result[st.peek()] = nums[i % size];//更新result + st.pop();//弹出栈顶 + } + st.push(i % size); + } + return result; } } - ``` + Python: ```python3 class Solution: @@ -128,3 +134,28 @@ class Solution: Go: JavaScript: + +```JS +/** + * @param {number[]} nums + * @return {number[]} + */ +var nextGreaterElements = function (nums) { + // let map = new Map(); + let stack = []; + let res = new Array(nums.length).fill(-1); + for (let i = 0; i < nums.length * 2; i++) { + while ( + stack.length && + nums[i % nums.length] > nums[stack[stack.length - 1]] + ) { + let index = stack.pop(); + res[index] = nums[i % nums.length]; + } + stack.push(i % nums.length); + } + + return res; +}; +``` +
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index dddac899..3b3d5056 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -88,7 +88,7 @@ dp[1] = 1; 以上我们用动规的方法分析完了,C++代码如下: -```C++ +```CPP class Solution { public: int fib(int N) { @@ -110,7 +110,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: int fib(int N) { @@ -137,7 +137,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: int fib(int N) { @@ -238,4 +238,4 @@ var fib = function(n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 7e48b53c..bca7e074 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -7,7 +7,7 @@

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

-## 513.找树左下角的值 +# 513.找树左下角的值 题目地址:[https://leetcode-cn.com/problems/find-bottom-left-tree-value/](https://leetcode-cn.com/problems/find-bottom-left-tree-value/v) @@ -21,7 +21,7 @@ ![513.找树左下角的值1](https://img-blog.csdnimg.cn/20210204153017586.png) -## 思路 +# 思路 本地要找出树的最后一行找到最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。 @@ -39,7 +39,7 @@ 如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。 -如果对二叉树深度和高度还有点疑惑的话,请看:[110.平衡二叉树](https://mp.weixin.qq.com/s/isUS-0HDYknmC0Rr4R8mww)。 +如果对二叉树深度和高度还有点疑惑的话,请看:[110.平衡二叉树](https://mp.weixin.qq.com/s/7QeWnxaAB66LjFJOs40XKg)。 所以要找深度最大的叶子节点。 @@ -55,7 +55,7 @@ 代码如下: -``` +```CPP int maxLen = INT_MIN; // 全局变量 记录最大深度 int maxleftValue; // 全局变量 最大深度最左节点的数值 void traversal(TreeNode* root, int leftLen) @@ -77,7 +77,7 @@ void traversal(TreeNode* root, int leftLen) 代码如下: -``` +```CPP if (root->left == NULL && root->right == NULL) { if (leftLen > maxLen) { maxLen = leftLen; // 更新最大深度 @@ -91,7 +91,7 @@ if (root->left == NULL && root->right == NULL) { 在找最大深度的时候,递归的过程中依然要使用回溯,代码如下: -```C++ +```CPP // 中 if (root->left) { // 左 leftLen++; // 深度加一 @@ -108,7 +108,7 @@ return; 完整代码如下: -```C++ +```CPP class Solution { public: int maxLen = INT_MIN; @@ -142,7 +142,7 @@ public: 当然回溯的地方可以精简,精简代码如下: -```C++ +```CPP class Solution { public: int maxLen = INT_MIN; @@ -170,7 +170,7 @@ public: }; ``` -如果对回溯部分精简的代码 不理解的话,可以看这篇[二叉树:找我的所有路径?](https://mp.weixin.qq.com/s/Osw4LQD2xVUnCJ-9jrYxJA)和[二叉树:以为使用了递归,其实还隐藏着回溯](https://mp.weixin.qq.com/s/ivLkHzWdhjQQD1rQWe6zWA) 。这两篇文章详细分析了回溯隐藏在了哪里。 +如果对回溯部分精简的代码 不理解的话,可以看这篇[257. 二叉树的所有路径](https://mp.weixin.qq.com/s/-x0IL-5eb9W0kZC1-TM0Lw) ## 迭代法 @@ -179,11 +179,11 @@ public: 只需要记录最后一行第一个节点的数值就可以了。 -如果对层序遍历不了解,看这篇[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog),这篇里也给出了层序遍历的模板,稍作修改就一过刷了这道题了。 +如果对层序遍历不了解,看这篇[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/4-bDKi7SdwfBGRm9FYduiA),这篇里也给出了层序遍历的模板,稍作修改就一过刷了这道题了。 代码如下: -```C++ +```CPP class Solution { public: int findBottomLeftValue(TreeNode* root) { @@ -205,20 +205,20 @@ public: }; ``` -## 总结 +# 总结 本题涉及如下几点: -* 递归求深度的写法,我们在[110.平衡二叉树](https://mp.weixin.qq.com/s/isUS-0HDYknmC0Rr4R8mww)中详细的分析了深度应该怎么求,高度应该怎么求。 -* 递归中其实隐藏了回溯,在[二叉树:以为使用了递归,其实还隐藏着回溯](https://mp.weixin.qq.com/s/ivLkHzWdhjQQD1rQWe6zWA)中讲解了究竟哪里使用了回溯,哪里隐藏了回溯。 -* 层次遍历,在[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog)深度讲解了二叉树层次遍历。 +* 递归求深度的写法,我们在[110.平衡二叉树](https://mp.weixin.qq.com/s/7QeWnxaAB66LjFJOs40XKg)中详细的分析了深度应该怎么求,高度应该怎么求。 +* 递归中其实隐藏了回溯,在[257. 二叉树的所有路径](https://mp.weixin.qq.com/s/-x0IL-5eb9W0kZC1-TM0Lw)中讲解了究竟哪里使用了回溯,哪里隐藏了回溯。 +* 层次遍历,在[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/4-bDKi7SdwfBGRm9FYduiA)深度讲解了二叉树层次遍历。 所以本题涉及到的点,我们之前都讲解过,这些知识点需要同学们灵活运用,这样就举一反三了。 -## 其他语言版本 +# 其他语言版本 -Java: +## Java ```java // 递归法 @@ -275,9 +275,9 @@ class Solution { -Python: +## Python -**递归 - 回溯** +递归: ```python class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: @@ -302,7 +302,8 @@ class Solution: __traverse(root, 0) return leftmost_val ``` -**迭代 - 层序遍历** + +迭代 - 层序遍历: ```python class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: @@ -322,19 +323,12 @@ class Solution: queue.append(cur.right) return result ``` -Go: -> 递归法 +## Go + +递归法: ```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ var maxDeep int // 全局变量 深度 var value int //全局变量 最终值 func findBottomLeftValue(root *TreeNode) int { @@ -366,17 +360,9 @@ func findLeftValue (root *TreeNode,deep int){ } ``` -> 迭代法 +迭代法: ```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ func findBottomLeftValue(root *TreeNode) int { queue:=list.New() var gradation int @@ -398,8 +384,10 @@ func findBottomLeftValue(root *TreeNode) int { } ``` -JavaScript: -1. 递归版本 +## JavaScript + +递归版本: + ```javascript var findBottomLeftValue = function(root) { //首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可 @@ -421,7 +409,8 @@ var findBottomLeftValue = function(root) { return resNode; }; ``` -2. 层序遍历 +层序遍历: + ```javascript var findBottomLeftValue = function(root) { //考虑层序遍历 记录最后一行的第一个节点 @@ -452,4 +441,4 @@ var findBottomLeftValue = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index 388d8d6a..54cea10e 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -69,7 +69,7 @@ 代码如下: -```C++ +```CPP if (s[i] == s[j]) { dp[i][j] = dp[i + 1][j - 1] + 2; } else { @@ -85,7 +85,7 @@ if (s[i] == s[j]) { 其他情况dp[i][j]初始为0就行,这样递推公式:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); 中dp[i][j]才不会被初始值覆盖。 -```C++ +```CPP vector> dp(s.size(), vector(s.size(), 0)); for (int i = 0; i < s.size(); i++) dp[i][i] = 1; ``` @@ -102,7 +102,7 @@ for (int i = 0; i < s.size(); i++) dp[i][i] = 1; 代码如下: -```C++ +```CPP for (int i = s.size() - 1; i >= 0; i--) { for (int j = i + 1; j < s.size(); j++) { if (s[i] == s[j]) { @@ -124,7 +124,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int longestPalindromeSubseq(string s) { @@ -243,4 +243,4 @@ const longestPalindromeSubseq = (s) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 3f907da9..ea2b1777 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -111,7 +111,7 @@ dp[j] (考虑coins[i]的组合总和) 就是所有的dp[j - coins[i]](不 代码如下: -```C++ +```CPP for (int i = 0; i < coins.size(); i++) { // 遍历物品 for (int j = coins[i]; j <= amount; j++) { // 遍历背包容量 dp[j] += dp[j - coins[i]]; @@ -151,7 +151,7 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int change(int amount, vector& coins) { @@ -265,4 +265,4 @@ const change = (amount, coins) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index bf646443..04c03b3f 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -39,7 +39,7 @@ 代码如下: -```C++ +```CPP class Solution { private: vector vec; @@ -75,7 +75,7 @@ public: 代码如下: -```C++ +```CPP class Solution { private: int result = INT_MAX; @@ -105,7 +105,7 @@ public: 下面我给出其中的一种中序遍历的迭代法,代码如下: -```C++ +```CPP class Solution { public: int getMinimumDifference(TreeNode* root) { @@ -337,4 +337,4 @@ var getMinimumDifference = function (root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 3ecb8195..d22cb4bc 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -107,7 +107,7 @@ traversal(cur->left); // 左 递归法整体代码如下: -```C++ +```CPP class Solution { private: int pre; // 记录前一个节点的数值 @@ -133,7 +133,7 @@ public: 这里我给出其中的一种,代码如下: -```C++ +```CPP class Solution { private: int pre; // 记录前一个节点的数值 @@ -308,4 +308,4 @@ var convertBST = function (root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 47e85161..ab1ef16a 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -46,7 +46,7 @@ https://leetcode-cn.com/problems/reverse-string-ii/ 使用C++库函数reverse的版本如下: -```C++ +```CPP class Solution { public: string reverseStr(string s, int k) { @@ -69,7 +69,7 @@ public: 下面我实现的reverse函数区间是左闭右闭区间,代码如下: -```C++ +```CPP class Solution { public: void reverse(string& s, int start, int end) { @@ -239,4 +239,4 @@ var reverseStr = function(s, k) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index 9aef2310..f6c13039 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -56,7 +56,7 @@ dp[i][0]:word2为空字符串,以i-1为结尾的字符串word2要删除多 dp[0][j]的话同理,所以代码如下: -```C++ +```CPP vector> dp(word1.size() + 1, vector(word2.size() + 1)); for (int i = 0; i <= word1.size(); i++) dp[i][0] = i; for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; @@ -78,7 +78,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 以上分析完毕,代码如下: -```C++ +```CPP class Solution { public: int minDistance(string word1, string word2) { @@ -181,4 +181,4 @@ const minDistance = (word1, word2) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 2900a817..b24a934b 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -90,7 +90,7 @@ return t1; 此时前序遍历,完整代码就写出来了,如下: -```C++ +```CPP class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { @@ -107,7 +107,7 @@ public: 那么中序遍历也是可以的,代码如下: -```C++ +```CPP class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { @@ -124,7 +124,7 @@ public: 后序遍历依然可以,代码如下: -```C++ +```CPP class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { @@ -145,7 +145,7 @@ public: 不修改输入树的结构,前序遍历,代码如下: -```C++ +```CPP class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { @@ -169,7 +169,7 @@ public: 本题我们也使用队列,模拟的层序遍历,代码如下: -```C++ +```CPP class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { @@ -216,7 +216,7 @@ public: 如下代码中,想要更改二叉树的值,应该传入指向指针的指针。 代码如下:(前序遍历) -```C++ +```CPP class Solution { public: void process(TreeNode** t1, TreeNode** t2) { @@ -504,4 +504,4 @@ var mergeTrees = function (root1, root2) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index d3b734ae..f81801b7 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -61,7 +61,7 @@ 以上三种情况分析完了,那么递归公式如下: -```C++ +```CPP if (s[i] == s[j]) { if (j - i <= 1) { // 情况一 和 情况二 result++; @@ -101,7 +101,7 @@ dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: 代码如下: -```C++ +```CPP for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 for (int j = i; j < s.size(); j++) { if (s[i] == s[j]) { @@ -129,7 +129,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int countSubstrings(string s) { @@ -154,7 +154,7 @@ public: ``` 以上代码是为了凸显情况一二三,当然是可以简洁一下的,如下: -```C++ +```CPP class Solution { public: int countSubstrings(string s) { @@ -192,7 +192,7 @@ public: **这两种情况可以放在一起计算,但分别计算思路更清晰,我倾向于分别计算**,代码如下: -```C++ +```CPP class Solution { public: int countSubstrings(string s) { @@ -412,4 +412,4 @@ const countSubstrings = (s) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0649.Dota2参议院.md b/problems/0649.Dota2参议院.md index 696124d6..b73686c4 100644 --- a/problems/0649.Dota2参议院.md +++ b/problems/0649.Dota2参议院.md @@ -78,7 +78,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 C++代码如下: -```C++ +```CPP class Solution { public: string predictPartyVictory(string senate) { @@ -136,5 +136,5 @@ public: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index 4e1e7a72..8c310a21 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -107,7 +107,7 @@ if (maxValueIndex < (nums.size() - 1)) { ``` 这样我们就分析完了,整体代码如下:(详细注释) -```C++ +```CPP class Solution { public: TreeNode* constructMaximumBinaryTree(vector& nums) { @@ -147,7 +147,7 @@ public: 优化后代码如下: -```C++ +```CPP class Solution { private: // 在左闭右开区间[left, right),构造二叉树 @@ -182,7 +182,7 @@ public: 可以发现上面的代码看上去简洁一些,**主要是因为第二版其实是允许空节点进入递归,所以不用在递归的时候加判断节点是否为空** 第一版递归过程:(加了if判断,为了不让空节点进入递归) -```C++ +```CPP if (maxValueIndex > 0) { // 这里加了判断是为了不让空节点进入递归 vector newVec(nums.begin(), nums.begin() + maxValueIndex); @@ -354,4 +354,4 @@ var constructMaximumBinaryTree = function (nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index 9b43ea6c..480e830f 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -48,7 +48,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: bool judgeCircle(string moves) { @@ -155,5 +155,5 @@ var judgeCircle = function(moves) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 26394eaa..267497b4 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -32,7 +32,7 @@ 不难写出如下代码: -```C++ +```CPP class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int high) { @@ -145,7 +145,7 @@ root->left = trimBST(root->left, low, high); 最后整体代码如下: -```C++ +```CPP class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int high) { @@ -167,7 +167,7 @@ public: 精简之后代码如下: -```C++ +```CPP class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int high) { @@ -195,7 +195,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: TreeNode* trimBST(TreeNode* root, int L, int R) { @@ -296,6 +296,7 @@ Go: * Right *TreeNode * } */ +// 递归 func trimBST(root *TreeNode, low int, high int) *TreeNode { if root==nil{ return nil @@ -312,6 +313,37 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode { root.Right=trimBST(root.Right,low,high) return root } +// 迭代 +func trimBST(root *TreeNode, low int, high int) *TreeNode { + if root == nil { + return nil + } + // 处理 root,让 root 移动到[low, high] 范围内,注意是左闭右闭 + for root != nil && (root.Valhigh){ + if root.Val < low{ + root = root.Right + }else{ + root = root.Left + } + } + cur := root + // 此时 root 已经在[low, high] 范围内,处理左孩子元素小于 low 的情况(左节点是一定小于 root.Val,因此天然小于 high) + for cur != nil{ + for cur.Left!=nil && cur.Left.Val < low{ + cur.Left = cur.Left.Right + } + cur = cur.Left + } + cur = root + // 此时 root 已经在[low, high] 范围内,处理右孩子大于 high 的情况 + for cur != nil{ + for cur.Right!=nil && cur.Right.Val > high{ + cur.Right = cur.Right.Left + } + cur = cur.Right + } + return root +} ``` @@ -373,4 +405,4 @@ var trimBST = function (root,low,high) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0673.最长递增子序列的个数.md b/problems/0673.最长递增子序列的个数.md index da0be740..ce3e8639 100644 --- a/problems/0673.最长递增子序列的个数.md +++ b/problems/0673.最长递增子序列的个数.md @@ -58,7 +58,7 @@ if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1); 代码如下: -```C++ +```CPP if (nums[i] > nums[j]) { if (dp[j] + 1 > dp[i]) { count[i] = count[j]; @@ -71,7 +71,7 @@ if (nums[i] > nums[j]) { 当然也可以这么写: -```C++ +```CPP if (nums[i] > nums[j]) { if (dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; // 更新dp[i]放在这里,就不用max了 @@ -88,7 +88,7 @@ if (nums[i] > nums[j]) { 代码如下: -```C++ +```CPP for (int i = 1; i < nums.size(); i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { @@ -131,7 +131,7 @@ dp[i] 是由0到i-1各个位置的最长升序子序列 推导而来,那么遍 j其实就是0到i-1,遍历i的循环里外层,遍历j则在内层,代码如下: -```C++ +```CPP for (int i = 1; i < nums.size(); i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { @@ -152,7 +152,7 @@ for (int i = 1; i < nums.size(); i++) { 代码如下: -```C++ +```CPP for (int i = 1; i < nums.size(); i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { @@ -184,7 +184,7 @@ for (int i = 0; i < nums.size(); i++) { 以上分析完毕,C++整体代码如下: -```C++ +```CPP class Solution { public: int findNumberOfLIS(vector& nums) { @@ -245,5 +245,5 @@ public: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 614bff72..0fe1c49a 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -73,7 +73,7 @@ 本文在确定递推公式的时候也说明了为什么本题只需要一层for循环,代码如下: -```C++ +```CPP for (int i = 0; i < nums.size() - 1; i++) { if (nums[i + 1] > nums[i]) { // 连续记录 dp[i + 1] = dp[i] + 1; // 递推公式 @@ -91,7 +91,7 @@ for (int i = 0; i < nums.size() - 1; i++) { 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int findLengthOfLCIS(vector& nums) { @@ -118,7 +118,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: int findLengthOfLCIS(vector& nums) { @@ -268,4 +268,4 @@ const findLengthOfLCIS = (nums) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md index 9215af5f..c9eb33c4 100644 --- a/problems/0684.冗余连接.md +++ b/problems/0684.冗余连接.md @@ -37,7 +37,7 @@ 这里整理出我的并查集模板如下: -```C++ +```CPP int n = 1005; // 节点数量3 到 1000 int father[1005]; @@ -88,7 +88,7 @@ bool same(int u, int v) { 并查集C++代码如下: -```C++ +```CPP class Solution { private: int n = 1005; // 节点数量3 到 1000 @@ -161,7 +161,6 @@ public: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
@@ -169,3 +168,4 @@ public: +
diff --git a/problems/0685.冗余连接II.md b/problems/0685.冗余连接II.md index b744c831..e282e620 100644 --- a/problems/0685.冗余连接II.md +++ b/problems/0685.冗余连接II.md @@ -58,7 +58,7 @@ 首先先计算节点的入度,代码如下: -```C++ +```CPP int inDegree[N] = {0}; // 记录节点入度 n = edges.size(); // 边的数量 for (int i = 0; i < n; i++) { @@ -70,7 +70,7 @@ for (int i = 0; i < n; i++) { 代码如下: -```C++ +```CPP vector vec; // 记录入度为2的边(如果有的话就两条边) // 找入度为2的节点所对应的边,注意要倒叙,因为优先返回最后出现在二维数组中的答案 for (int i = n - 1; i >= 0; i--) { @@ -112,7 +112,7 @@ vector getRemoveEdge(const vector>& edges) 本题C++代码如下:(详细注释了) -```C++ +```CPP class Solution { private: static const int N = 1010; // 如题:二维数组大小的在3到1000范围内 @@ -224,6 +224,6 @@ public: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 4b02f8af..68d30e3f 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -79,7 +79,7 @@ return NULL; 整体代码如下: -```C++ +```CPP class Solution { public: TreeNode* searchBST(TreeNode* root, int val) { @@ -109,7 +109,7 @@ public: 所以迭代法代码如下: -```C++ +```CPP class Solution { public: TreeNode* searchBST(TreeNode* root, int val) { @@ -364,4 +364,4 @@ var searchBST = function (root, val) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 1ec80da1..fdaba43e 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -96,7 +96,7 @@ return root; 整体代码如下: -```C++ +```CPP class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { @@ -126,7 +126,7 @@ void traversal(TreeNode* cur, int val) 代码如下: -```C++ +```CPP class Solution { private: TreeNode* parent; @@ -172,7 +172,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { @@ -487,4 +487,4 @@ var insertIntoBST = function (root, val) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 1bbb4b4f..2efbd7c4 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -63,7 +63,7 @@ 代码如下:(详细注释) -```C++ +```CPP // 版本一 class Solution { public: @@ -102,7 +102,7 @@ public: 代码如下:(详细注释) -```C++ +```CPP // 版本二 class Solution { public: @@ -412,4 +412,4 @@ func search(nums: [Int], target: Int) -> Int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 5e748a24..0aa038e8 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -56,7 +56,7 @@ https://leetcode-cn.com/problems/design-linked-list/ ## 代码 -```C++ +```CPP class MyLinkedList { public: // 定义链表节点结构体 @@ -954,4 +954,4 @@ class MyLinkedList { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index 2e8f9208..6dc95a2d 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -60,7 +60,7 @@ 贪心算法C++代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices, int fee) { @@ -101,7 +101,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices, int fee) { @@ -126,7 +126,7 @@ public: C++ 代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices, int fee) { @@ -246,4 +246,4 @@ var maxProfit = function(prices, fee) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index e2b83999..53952a7f 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -72,7 +72,7 @@ dp[i][1] 表示第i天不持有股票所得最多现金 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices, int fee) { @@ -171,4 +171,4 @@ const maxProfit = (prices,fee) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 959ed39e..2fe38f0a 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -74,7 +74,7 @@ dp[i][j] :以下标i - 1为结尾的A,和以下标j - 1为结尾的B,最 代码如下: -```C++ +```CPP for (int i = 1; i <= A.size(); i++) { for (int j = 1; j <= B.size(); j++) { if (A[i - 1] == B[j - 1]) { @@ -94,7 +94,7 @@ for (int i = 1; i <= A.size(); i++) { 以上五部曲分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int findLength(vector& A, vector& B) { @@ -128,7 +128,7 @@ public: **此时遍历B数组的时候,就要从后向前遍历,这样避免重复覆盖**。 -```C++ +```CPP class Solution { public: int findLength(vector& A, vector& B) { @@ -285,4 +285,4 @@ const findLength = (A, B) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0724.寻找数组的中心索引.md b/problems/0724.寻找数组的中心索引.md index 09966e05..3ed68d47 100644 --- a/problems/0724.寻找数组的中心索引.md +++ b/problems/0724.寻找数组的中心索引.md @@ -43,7 +43,7 @@ * 判断leftSum和rightSum是否相同 C++代码如下: -```C++ +```CPP class Solution { public: int pivotIndex(vector& nums) { @@ -106,5 +106,5 @@ class Solution { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 75c7f250..7cb24236 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -34,7 +34,7 @@ 题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时! 代码如下: -```C++ +```CPP class Solution { private: bool checkNum(int num) { @@ -87,7 +87,7 @@ public: C++代码如下: -```C++ +```CPP class Solution { public: int monotoneIncreasingDigits(int N) { @@ -190,4 +190,4 @@ var monotoneIncreasingDigits = function(n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 8ad79fe3..ddcbe428 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -117,7 +117,7 @@ T[4]弹出之后, T[5] > T[3] (当前遍历的元素T[i]大于栈顶元素T[ C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -148,7 +148,7 @@ public: 精简代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -283,5 +283,5 @@ func dailyTemperatures(num []int) []int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 4238a389..9b691c5f 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -98,7 +98,7 @@ dp[1] = cost[1]; 以上分析完毕,整体C++代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -120,7 +120,7 @@ public: 还可以优化空间复杂度,因为dp[i]就是由前两位推出来的,那么也不用dp数组了,C++代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -165,7 +165,7 @@ public: 所以代码这么写: -```C++ +```CPP class Solution { public: int minCostClimbingStairs(vector& cost) { @@ -272,4 +272,4 @@ var minCostClimbingStairs = function(cost) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index b36e00b7..ccf654d4 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -47,7 +47,7 @@ 明白原理之后,代码并不复杂,如下: -```C++ +```CPP class Solution { public: vector partitionLabels(string S) { @@ -181,4 +181,4 @@ var partitionLabels = function(s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index 1b3c4a95..bd7c99c9 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.md @@ -54,7 +54,7 @@ BFS C++代码代码如下: -```C++ +```CPP class Solution { bool bfs(const vector>& rooms) { vector visited(rooms.size(), 0); // 标记房间是否被访问过 @@ -89,7 +89,7 @@ public: DFS C++代码如下: -```C++ +```CPP class Solution { private: void dfs(int key, const vector>& rooms, vector& visited) { @@ -131,5 +131,5 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index 4b4ff63b..32232c4a 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -50,7 +50,7 @@ 代码如下: -```C++ +```CPP class Solution { public: bool backspaceCompare(string S, string T) { @@ -78,7 +78,7 @@ public: 当然以上代码,大家可以发现有重复的逻辑处理S,处理T,可以把这块公共逻辑抽离出来,代码精简如下: -```C++ +```CPP class Solution { private: string getString(const string& S) { @@ -115,7 +115,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: bool backspaceCompare(string S, string T) { @@ -196,5 +196,5 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index 0d5a7075..792bee9a 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -82,7 +82,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: bool lemonadeChange(vector& bills) { @@ -260,4 +260,4 @@ var lemonadeChange = function(bills) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md index d4c1be7a..92db204d 100644 --- a/problems/0922.按奇偶排序数组II.md +++ b/problems/0922.按奇偶排序数组II.md @@ -32,7 +32,7 @@ 其实这道题可以用很朴实的方法,时间复杂度就就是O(n)了,C++代码如下: -```C++ +```CPP class Solution { public: vector sortArrayByParityII(vector& A) { @@ -64,7 +64,7 @@ public: 以上代码我是建了两个辅助数组,而且A数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助树,代码如下: -```C++ +```CPP class Solution { public: vector sortArrayByParityII(vector& A) { @@ -93,7 +93,7 @@ public: 当然还可以在原数组上修改,连result数组都不用了。 -```C++ +```CPP class Solution { public: vector sortArrayByParityII(vector& A) { @@ -166,5 +166,5 @@ class Solution { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 95b6325a..4d3543f4 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -59,7 +59,7 @@ 上面的逻辑想清楚了,不难写出如下C++代码: -```C++ +```CPP class Solution { public: bool isLongPressedName(string name, string typed) { @@ -109,5 +109,5 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index bf3f232e..98344949 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -122,7 +122,7 @@ class Solution { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index a0eb5883..380ca8f8 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -216,7 +216,7 @@ int minCameraCover(TreeNode* root) { ## C++代码 -```C++ +```CPP // 版本一 class Solution { private: @@ -270,7 +270,7 @@ public: 在以上代码的基础上,再进行精简,代码如下: -```C++ +```CPP // 版本二 class Solution { private: @@ -411,4 +411,4 @@ var minCameraCover = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 7206fd6f..9e71ec0d 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -29,7 +29,7 @@ https://leetcode-cn.com/problems/squares-of-a-sorted-array/ 最直观的相反,莫过于:每个数平方之后,排个序,美滋滋,代码如下: -```C++ +```CPP class Solution { public: vector sortedSquares(vector& A) { @@ -64,7 +64,7 @@ public: 不难写出如下代码: -```C++ +```CPP class Solution { public: vector sortedSquares(vector& A) { @@ -258,4 +258,4 @@ func sortedSquares(_ nums: [Int]) -> [Int] { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 19f9e128..82dbf4cf 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -97,7 +97,7 @@ for (int i = 0; i < 26; i++) { 整体C++代码如下: -```C++ +```CPP class Solution { public: vector commonChars(vector& A) { @@ -237,4 +237,4 @@ var commonChars = function (words) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 3534e1e2..a689fbf7 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -61,7 +61,7 @@ 对应C++代码如下: -```C++ +```CPP class Solution { static bool cmp(int a, int b) { return abs(a) > abs(b); @@ -219,4 +219,4 @@ var largestSumAfterKNegations = function(nums, k) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index 3a0e6a68..3172cfc7 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -45,7 +45,7 @@ 本题代码如下: -```C++ +```CPP class Solution { public: int maxUncrossedLines(vector& A, vector& B) { @@ -139,4 +139,4 @@ const maxUncrossedLines = (nums1, nums2) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index c6a49376..c4ae85c9 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -69,7 +69,7 @@ https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/ C++代码 : -```C++ +```CPP class Solution { public: string removeDuplicates(string S) { @@ -97,7 +97,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: string removeDuplicates(string S) { @@ -252,4 +252,4 @@ var removeDuplicates = function(s) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index c09e476a..7b71eae4 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -93,7 +93,7 @@ vector dp(15001, 0); 代码如下: -```C++ +```CPP for (int i = 0; i < stones.size(); i++) { // 遍历物品 for (int j = target; j >= stones[i]; j--) { // 遍历背包 dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]); @@ -119,7 +119,7 @@ for (int i = 0; i < stones.size(); i++) { // 遍历物品 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int lastStoneWeightII(vector& stones) { @@ -225,4 +225,4 @@ func max(a, b int) int { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index e1fc1abb..c0468d08 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -65,7 +65,7 @@ dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符 代码如下: -```C++ +```CPP if (text1[i - 1] == text2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { @@ -107,7 +107,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 以上分析完毕,C++代码如下: -```C++ +```CPP class Solution { public: int longestCommonSubsequence(string text1, string text2) { @@ -221,4 +221,4 @@ const longestCommonSubsequence = (text1, text2) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index 32aa3c3a..1c09ee8a 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -53,7 +53,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: bool uniqueOccurrences(vector& arr) { @@ -123,5 +123,5 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index a3d724fa..300aa0e2 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -68,7 +68,7 @@ sort(vec.begin(), vec.end()); // 从小到大排序之后,元素下标就是 这里就需要一个技巧了,**在构造数组hash的时候,从后向前遍历,这样hash里存放的就是相同元素最左面的数值和下标了**。 代码如下: -```C++ +```CPP int hash[101]; for (int i = vec.size() - 1; i >= 0; i--) { // 从后向前,记录 vec[i] 对应的下标 hash[vec[i]] = i; @@ -79,7 +79,7 @@ for (int i = vec.size() - 1; i >= 0; i--) { // 从后向前,记录 vec[i] 对 代码如下: -```C++ +```CPP // 此时hash里保存的每一个元素数值 对应的 小于这个数值的个数 for (int i = 0; i < nums.size(); i++) { vec[i] = hash[nums[i]]; @@ -92,7 +92,7 @@ for (int i = 0; i < nums.size(); i++) { 关键地方讲完了,整体C++代码如下: -```C++ +```CPP class Solution { public: vector smallerNumbersThanCurrent(vector& nums) { @@ -184,5 +184,5 @@ class Solution { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 30241c9b..156efea6 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -69,7 +69,7 @@ sort(vec.begin(), vec.end()); // 从小到大排序之后,元素下标就是 这里就需要一个技巧了,**在构造数组hash的时候,从后向前遍历,这样hash里存放的就是相同元素最左面的数值和下标了**。 代码如下: -```C++ +```CPP int hash[101]; for (int i = vec.size() - 1; i >= 0; i--) { // 从后向前,记录 vec[i] 对应的下标 hash[vec[i]] = i; @@ -80,7 +80,7 @@ for (int i = vec.size() - 1; i >= 0; i--) { // 从后向前,记录 vec[i] 对 代码如下: -```C++ +```CPP // 此时hash里保存的每一个元素数值 对应的 小于这个数值的个数 for (int i = 0; i < nums.size(); i++) { vec[i] = hash[nums[i]]; @@ -93,7 +93,7 @@ for (int i = 0; i < nums.size(); i++) { 关键地方讲完了,整体C++代码如下: -```C++ +```CPP class Solution { public: vector smallerNumbersThanCurrent(vector& nums) { @@ -162,4 +162,4 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index 268f21c5..b9d4fb65 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -42,7 +42,7 @@ 代码如下: -```C++ +```CPP class Solution { private: vector vec; @@ -87,6 +87,6 @@ JavaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md index 2b995c59..6897c01f 100644 --- a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md +++ b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md @@ -67,7 +67,7 @@ 实现三个函数,时间复杂度分别是 O(n) , O(n^2), O(nlogn),使用加法运算来统一测试。 -```C++ +```CPP // O(n) void function1(long long n) { long long k = 0; @@ -78,7 +78,7 @@ void function1(long long n) { ``` -```C++ +```CPP // O(n^2) void function2(long long n) { long long k = 0; @@ -91,7 +91,7 @@ void function2(long long n) { } ``` -```C++ +```CPP // O(nlogn) void function3(long long n) { long long k = 0; @@ -105,7 +105,7 @@ void function3(long long n) { ``` 来看一下这三个函数随着n的规模变化,耗时会产生多大的变化,先测function1 ,就把 function2 和 function3 注释掉 -```C++ +```CPP int main() { long long n; // 数据规模 while (1) { @@ -154,7 +154,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 # 完整测试代码 -```C++ +```CPP #include #include #include @@ -235,4 +235,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md index 353bd68e..4a0ef0d2 100644 --- a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md +++ b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md @@ -58,7 +58,7 @@ else dp[i][j] = dp[i][j - 1]; 状态转移方程: -```C++ +```CPP if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } else { @@ -89,7 +89,7 @@ if (s[i - 1] == t[j - 1]) { 那最后当然是取最小值,所以当word1[i - 1] 与 word2[j - 1]不相同的时候,递推公式:dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1}); 状态转移方程: -```C++ +```CPP if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { @@ -150,7 +150,7 @@ if (word1[i - 1] != word2[j - 1]),此时就需要编辑了,如何编辑呢 递归公式代码如下: -```C++ +```CPP if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } @@ -181,4 +181,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index bd987f12..0f768e0f 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -19,7 +19,7 @@ 那么如下我再给出求100. 相同的树 的代码,如下: -```C++ +```CPP class Solution { public: bool compare(TreeNode* tree1, TreeNode* tree2) { @@ -52,7 +52,7 @@ public: 如下的代码充分的体现出回溯:(257. 二叉树的所有路径) -```C++ +```CPP class Solution { private: @@ -120,13 +120,13 @@ public: 为了把这份精简代码的回溯过程展现出来,大家可以试一试把: -```C++ +```CPP if (cur->left) traversal(cur->left, path + "->", result); // 左 回溯就隐藏在这里 ``` 改成如下代码: -```C++ +```CPP path += "->"; traversal(cur->left, path, result); // 左 ``` @@ -149,7 +149,7 @@ if (cur->right) { 如果想把回溯加上,就要 在上面代码的基础上,加上回溯,就可以AC了。 -```C++ +```CPP if (cur->left) { path += "->"; traversal(cur->left, path, result); // 左 @@ -448,4 +448,4 @@ func traversal(root *TreeNode,result *[]string,path *[]int){ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/二叉树总结篇.md b/problems/二叉树总结篇.md index d4af8aae..43276ec9 100644 --- a/problems/二叉树总结篇.md +++ b/problems/二叉树总结篇.md @@ -174,4 +174,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index a720672e..abeeae67 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -229,4 +229,4 @@ type TreeNode struct { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 9e24fef6..d105f4b8 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -34,7 +34,7 @@ 中序遍历代码如下:(详细注释) -```C++ +```CPP class Solution { public: vector inorderTraversal(TreeNode* root) { @@ -77,7 +77,7 @@ public: 迭代法前序遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) -```C++ +```CPP class Solution { public: vector preorderTraversal(TreeNode* root) { @@ -108,7 +108,7 @@ public: 后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) -```C++ +```CPP class Solution { public: vector postorderTraversal(TreeNode* root) { @@ -530,4 +530,4 @@ var postorderTraversal = function(root, res = []) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index 20397773..faa580e0 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -37,7 +37,7 @@ 不难写出如下代码: (**注意代码中空节点不入栈**) -```C++ +```CPP class Solution { public: vector preorderTraversal(TreeNode* root) { @@ -84,7 +84,7 @@ public: **中序遍历,可以写出如下代码:** -```C++ +```CPP class Solution { public: vector inorderTraversal(TreeNode* root) { @@ -116,7 +116,7 @@ public: **所以后序遍历只需要前序遍历的代码稍作修改就可以了,代码如下:** -```C++ +```CPP class Solution { public: vector postorderTraversal(TreeNode* root) { @@ -473,4 +473,4 @@ var postorderTraversal = function(root, res = []) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 220ccf65..223cf722 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -58,7 +58,7 @@ traversal(cur->right, vec); // 右 前序遍历: -```C++ +```CPP class Solution { public: void traversal(TreeNode* cur, vector& vec) { @@ -79,7 +79,7 @@ public: 中序遍历: -```C++ +```CPP void traversal(TreeNode* cur, vector& vec) { if (cur == NULL) return; traversal(cur->left, vec); // 左 @@ -90,7 +90,7 @@ void traversal(TreeNode* cur, vector& vec) { 后序遍历: -```C++ +```CPP void traversal(TreeNode* cur, vector& vec) { if (cur == NULL) return; traversal(cur->left, vec); // 左 @@ -367,4 +367,4 @@ var postorderTraversal = function(root) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/关于时间复杂度,你不知道的都在这里!.md b/problems/关于时间复杂度,你不知道的都在这里!.md index fe378228..7ff9b470 100644 --- a/problems/关于时间复杂度,你不知道的都在这里!.md +++ b/problems/关于时间复杂度,你不知道的都在这里!.md @@ -177,4 +177,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md b/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md index fbcdf970..c5797739 100644 --- a/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md +++ b/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md @@ -221,4 +221,4 @@ leetcode是专门针对算法练习的题库,leetcode现在也推出了中文 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md b/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md index f6218777..d9cc8d45 100644 --- a/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md +++ b/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md @@ -67,7 +67,7 @@ 实现三个函数,时间复杂度分别是 O(n) , O(n^2), O(nlogn),使用加法运算来统一测试。 -```C++ +```CPP // O(n) void function1(long long n) { long long k = 0; @@ -78,7 +78,7 @@ void function1(long long n) { ``` -```C++ +```CPP // O(n^2) void function2(long long n) { long long k = 0; @@ -91,7 +91,7 @@ void function2(long long n) { } ``` -```C++ +```CPP // O(nlogn) void function3(long long n) { long long k = 0; @@ -105,7 +105,7 @@ void function3(long long n) { ``` 来看一下这三个函数随着n的规模变化,耗时会产生多大的变化,先测function1 ,就把 function2 和 function3 注释掉 -```C++ +```CPP int main() { long long n; // 数据规模 while (1) { @@ -154,7 +154,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 # 完整测试代码 -```C++ +```CPP #include #include #include @@ -228,4 +228,4 @@ int main() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/上海互联网公司总结.md b/problems/前序/上海互联网公司总结.md index 386a0a93..05cbe1b0 100644 --- a/problems/前序/上海互联网公司总结.md +++ b/problems/前序/上海互联网公司总结.md @@ -133,4 +133,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md index 5a19a72a..3c5fb4e4 100644 --- a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md +++ b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md @@ -13,7 +13,7 @@ 而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样: -```C++ +```CPP class Solution { public: int minimumTotal(vector>& triangle) { @@ -55,7 +55,7 @@ public: 这道题如果要是力扣上的核心代码模式,OJ应该直接给出如下代码: -```C++ +```CPP class Solution { public: int getDays(vector& work, vector& gym) { @@ -72,7 +72,7 @@ ACM模式要求写出来的代码是直接可以本地运行的,所以我们 拿本题来说,为了让代码可以运行,需要include这些库函数: -```C++ +```CPP #include #include using namespace std; @@ -87,7 +87,7 @@ using namespace std; 完整代码如下: -```C++ +```CPP #include #include using namespace std; @@ -122,4 +122,4 @@ int main() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/代码风格.md b/problems/前序/代码风格.md index a9eabd0c..a71ac420 100644 --- a/problems/前序/代码风格.md +++ b/problems/前序/代码风格.md @@ -108,7 +108,7 @@ while (n) { ``` 以下是我刚写的力扣283.移动零的代码,大家可以看一下整体风格,注意空格的细节! -```C++ +```CPP class Solution { public: void moveZeroes(vector& nums) { @@ -149,4 +149,4 @@ Google规范是 大括号和 控制语句保持同一行的,我个人也很认 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/关于时间复杂度,你不知道的都在这里!.md b/problems/前序/关于时间复杂度,你不知道的都在这里!.md index d6471b99..d4869b14 100644 --- a/problems/前序/关于时间复杂度,你不知道的都在这里!.md +++ b/problems/前序/关于时间复杂度,你不知道的都在这里!.md @@ -173,4 +173,4 @@ O(2 * n^2 + 10 * n + 1000) < O(3 * n^2),所以说最后省略掉常数项系 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/关于空间复杂度,可能有几个疑问?.md b/problems/前序/关于空间复杂度,可能有几个疑问?.md index 0208aa91..0f25f378 100644 --- a/problems/前序/关于空间复杂度,可能有几个疑问?.md +++ b/problems/前序/关于空间复杂度,可能有几个疑问?.md @@ -42,7 +42,7 @@ 来看一下例子,什么时候的空间复杂度是O(1)呢,C++代码如下: -```C++ +```CPP int j = 0; for (int i = 0; i < n; i++) { j++; @@ -54,7 +54,7 @@ for (int i = 0; i < n; i++) { 什么时候的空间复杂度是O(n)? 当消耗空间和输入参数n保持线性增长,这样的空间复杂度为O(n),来看一下这段C++代码 -```C++ +```CPP int* a = new int(n); for (int i = 0; i < n; i++) { a[i] = i; @@ -76,4 +76,4 @@ for (int i = 0; i < n; i++) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md b/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md index 6d3b4931..3fccfb22 100644 --- a/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md +++ b/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md @@ -77,7 +77,7 @@ 可以看一下这段C++代码输出的各个数据类型大小是多少? -```C++ +```CPP struct node{ int num; char cha; @@ -153,4 +153,4 @@ char型的数据和int型的数据挨在一起,该int数据从地址1开始, * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/力扣上的代码想在本地编译运行?.md b/problems/前序/力扣上的代码想在本地编译运行?.md index f4a9b0f3..a0fcbcd8 100644 --- a/problems/前序/力扣上的代码想在本地编译运行?.md +++ b/problems/前序/力扣上的代码想在本地编译运行?.md @@ -27,7 +27,7 @@ 力扣746. 使用最小花费爬楼梯,完整的可以在直接本地运行的C++代码如下: -```C++ +```CPP #include #include using namespace std; @@ -70,4 +70,4 @@ int main() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/北京互联网公司总结.md b/problems/前序/北京互联网公司总结.md index 4dcaa691..c770759f 100644 --- a/problems/前序/北京互联网公司总结.md +++ b/problems/前序/北京互联网公司总结.md @@ -119,4 +119,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/广州互联网公司总结.md b/problems/前序/广州互联网公司总结.md index e9b2af00..3cc18c8e 100644 --- a/problems/前序/广州互联网公司总结.md +++ b/problems/前序/广州互联网公司总结.md @@ -82,4 +82,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/成都互联网公司总结.md b/problems/前序/成都互联网公司总结.md index 2435ccb2..d44800cd 100644 --- a/problems/前序/成都互联网公司总结.md +++ b/problems/前序/成都互联网公司总结.md @@ -80,4 +80,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/杭州互联网公司总结.md b/problems/前序/杭州互联网公司总结.md index e2691469..aebf5779 100644 --- a/problems/前序/杭州互联网公司总结.md +++ b/problems/前序/杭州互联网公司总结.md @@ -91,4 +91,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/深圳互联网公司总结.md b/problems/前序/深圳互联网公司总结.md index 4b68dad6..9e089315 100644 --- a/problems/前序/深圳互联网公司总结.md +++ b/problems/前序/深圳互联网公司总结.md @@ -85,4 +85,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/程序员写文档工具.md b/problems/前序/程序员写文档工具.md index b06ce0ad..b76fb036 100644 --- a/problems/前序/程序员写文档工具.md +++ b/problems/前序/程序员写文档工具.md @@ -140,4 +140,4 @@ Markdown支持部分html,例如这样 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/程序员简历.md b/problems/前序/程序员简历.md index a9abcdc4..f47516dc 100644 --- a/problems/前序/程序员简历.md +++ b/problems/前序/程序员简历.md @@ -136,4 +136,4 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/递归算法的时间与空间复杂度分析.md b/problems/前序/递归算法的时间与空间复杂度分析.md index f1501e8a..b6c79f25 100644 --- a/problems/前序/递归算法的时间与空间复杂度分析.md +++ b/problems/前序/递归算法的时间与空间复杂度分析.md @@ -18,7 +18,7 @@ 先来看一下求斐波那契数的递归写法。 -```C++ +```CPP int fibonacci(int i) { if(i <= 0) return 0; if(i == 1) return 1; @@ -50,7 +50,7 @@ int fibonacci(int i) { 以下为C++代码,来测一下,让我们输入n的时候,这段递归求斐波那契代码的耗时。 -```C++ +```CPP #include #include #include @@ -99,7 +99,7 @@ int main() 其实罪魁祸首就是这里的两次递归,导致了时间复杂度以指数上升。 -```C++ +```CPP return fibonacci(i-1) + fibonacci(i-2); ``` @@ -107,7 +107,7 @@ return fibonacci(i-1) + fibonacci(i-2); 来看一下如下代码: -```C++ +```CPP // 版本二 int fibonacci(int first, int second, int n) { if (n <= 0) { @@ -138,7 +138,7 @@ int fibonacci(int first, int second, int n) { 此时再来测一下耗时情况验证一下: -```C++ +```CPP #include #include #include @@ -208,7 +208,7 @@ int main() 那么每次递归的空间复杂度是O(1), 调用栈深度为n,所以这段递归代码的空间复杂度就是O(n)。 -```C++ +```CPP int fibonacci(int i) { if(i <= 0) return 0; if(i == 1) return 1; @@ -227,7 +227,7 @@ int fibonacci(int i) { 带大家再分析一段二分查找的递归实现。 -```C++ +```CPP int binary_search( int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; @@ -272,4 +272,4 @@ int binary_search( int arr[], int l, int r, int x) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md b/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md index 5a75b6c7..b3aef43c 100644 --- a/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md +++ b/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md @@ -27,7 +27,7 @@ 想一下这么简单的一道题目,代码应该如何写呢。最直观的方式应该就是,一个for循环求出结果,代码如下: -```C++ +```CPP int function1(int x, int n) { int result = 1; // 注意 任何数的0次方等于1 for (int i = 0; i < n; i++) { @@ -44,7 +44,7 @@ int function1(int x, int n) { 那么就可以写出了如下这样的一个递归的算法,使用递归解决了这个问题。 -```C++ +```CPP int function2(int x, int n) { if (n == 0) { return 1; // return 1 同样是因为0次方是等于1的 @@ -62,7 +62,7 @@ int function2(int x, int n) { 这个时间复杂度就没有达到面试官的预期。于是又写出了如下的递归算法的代码: -```C++ +```CPP int function3(int x, int n) { if (n == 0) { return 1; @@ -101,7 +101,7 @@ int function3(int x, int n) { 于是又写出如下递归算法的代码: -```C++ +```CPP int function4(int x, int n) { if (n == 0) { return 1; @@ -132,7 +132,7 @@ int function4(int x, int n) { 对于function3 这样的递归实现,很容易让人感觉这是O(logn)的时间复杂度,其实这是O(n)的算法! -```C++ +```CPP int function3(int x, int n) { if (n == 0) { return 1; @@ -155,4 +155,4 @@ int function3(int x, int n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 3e4e3631..4ae5f9f2 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -46,7 +46,7 @@ i指向新长度的末尾,j指向旧长度的末尾。 C++代码如下: -```C++ +```CPP class Solution { public: string replaceSpace(string s) { @@ -288,4 +288,4 @@ javaScript: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index fe4c3473..76908f2d 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -58,7 +58,7 @@ https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/ C++代码如下: -```C++ +```CPP class Solution { public: string reverseLeftWords(string s, int n) { @@ -189,4 +189,4 @@ var reverseLeftWords = function (s, n) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/动态规划-股票问题总结篇.md b/problems/动态规划-股票问题总结篇.md index 590a8008..d186aaf9 100644 --- a/problems/动态规划-股票问题总结篇.md +++ b/problems/动态规划-股票问题总结篇.md @@ -24,7 +24,7 @@ 【贪心解法】 取最左最小值,取最右最大值,那么得到的差值就是最大利润,代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -56,7 +56,7 @@ public: 代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -79,7 +79,7 @@ public: 使用滚动数组,代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -110,7 +110,7 @@ public: 收集每天的正利润便可,代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -146,7 +146,7 @@ dp数组定义: 代码如下:(注意代码中的注释,标记了和121.买卖股票的最佳时机唯一不同的地方) -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -204,7 +204,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -230,7 +230,7 @@ public: 当然,大家可以看到力扣官方题解里的一种优化空间写法,我这里给出对应的C++版本: -```C++ +```CPP // 版本二 class Solution { public: @@ -291,7 +291,7 @@ dp[i][2] = max(dp[i - 1][i] + prices[i], dp[i][2]) 同理可以类比剩下的状态,代码如下: -```C++ +```CPP for (int j = 0; j < 2 * k - 1; j += 2) { dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]); @@ -300,7 +300,7 @@ for (int j = 0; j < 2 * k - 1; j += 2) { 整体代码如下: -```C++ +```CPP class Solution { public: int maxProfit(int k, vector& prices) { @@ -375,7 +375,7 @@ p[i][3] = dp[i - 1][2]; 综上分析,递推代码如下: -```C++ +```CPP dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3]- prices[i], dp[i - 1][1]) - prices[i]; dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); dp[i][2] = dp[i - 1][0] + prices[i]; @@ -384,7 +384,7 @@ dp[i][3] = dp[i - 1][2]; 整体代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -439,7 +439,7 @@ dp[i][1] 表示第i天不持有股票所得最多现金 以上分析完毕,代码如下: -```C++ +```CPP class Solution { public: int maxProfit(vector& prices, int fee) { @@ -486,4 +486,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/动态规划总结篇.md b/problems/动态规划总结篇.md index 797f426a..d1c52005 100644 --- a/problems/动态规划总结篇.md +++ b/problems/动态规划总结篇.md @@ -136,5 +136,5 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/动态规划理论基础.md b/problems/动态规划理论基础.md index 250fa57d..c9491da1 100644 --- a/problems/动态规划理论基础.md +++ b/problems/动态规划理论基础.md @@ -140,4 +140,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/双指针总结.md b/problems/双指针总结.md index b03d3ff2..df7cffae 100644 --- a/problems/双指针总结.md +++ b/problems/双指针总结.md @@ -100,4 +100,4 @@ for (int i = 0; i < array.size(); i++) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/周总结/20200927二叉树周末总结.md b/problems/周总结/20200927二叉树周末总结.md index a6d73c01..f81cc6bb 100644 --- a/problems/周总结/20200927二叉树周末总结.md +++ b/problems/周总结/20200927二叉树周末总结.md @@ -13,7 +13,7 @@ 对于二叉树节点的定义,C++代码如下: -```C++ +```CPP struct TreeNode { int val; TreeNode *left; @@ -35,7 +35,7 @@ TreeNode* a = new TreeNode(9); 没有构造函数的话就要这么写: -```C++ +```CPP TreeNode* a = new TreeNode(); a->val = 9; a->left = NULL; @@ -66,7 +66,7 @@ morris遍历是二叉树遍历算法的超强进阶算法,morris遍历可以 拿前序遍历来举例,空节点入栈: -```C++ +```CPP class Solution { public: vector preorderTraversal(TreeNode* root) { @@ -88,7 +88,7 @@ public: 前序遍历空节点不入栈的代码:(注意注释部分和上文的区别) -```C++ +```CPP class Solution { public: vector preorderTraversal(TreeNode* root) { @@ -203,3 +203,4 @@ public: **本周我们都是讲解了二叉树,从理论基础到遍历方式,从递归到迭代,从深度遍历到广度遍历,最后再用了一个翻转二叉树的题目把我们之前讲过的遍历方式都串了起来。** +
diff --git a/problems/周总结/20201003二叉树周末总结.md b/problems/周总结/20201003二叉树周末总结.md index e69e36f8..db89fc82 100644 --- a/problems/周总结/20201003二叉树周末总结.md +++ b/problems/周总结/20201003二叉树周末总结.md @@ -21,7 +21,7 @@ 100.相同的树的递归代码如下: -```C++ +```CPP class Solution { public: bool compare(TreeNode* left, TreeNode* right) { @@ -48,7 +48,7 @@ public: 100.相同的树,精简之后代码如下: -```C++ +```CPP class Solution { public: bool compare(TreeNode* left, TreeNode* right) { @@ -67,7 +67,7 @@ public: 100.相同的树,迭代法代码如下: -```C++ +```CPP class Solution { public: @@ -109,7 +109,7 @@ public: **而根节点的高度就是二叉树的最大深度**,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度,所以[二叉树:看看这些树的最大深度](https://mp.weixin.qq.com/s/guKwV-gSNbA1CcbvkMtHBg)中使用的是后序遍历。 本题当然也可以使用前序,代码如下:(**充分表现出求深度回溯的过程**) -```C++ +```CPP class Solution { public: int result; @@ -143,7 +143,7 @@ public: 注意以上代码是为了把细节体现出来,简化一下代码如下: -```C++ +```CPP class Solution { public: int result; @@ -223,12 +223,12 @@ public: 文中我明确的说了:**回溯就隐藏在traversal(cur->left, path + "->", result);中的 path + "->"。 每次函数调用完,path依然是没有加上"->" 的,这就是回溯了。** 如果还不理解的话,可以把 -```C++ +```CPP traversal(cur->left, path + "->", result); ``` 改成 -```C++ +```CPP string tmp = path + "->"; traversal(cur->left, tmp, result); ``` @@ -255,3 +255,4 @@ traversal(cur->left, tmp, result); * 知乎:[代码随想录](https://www.zhihu.com/people/sun-xiu-yang-64) ![](https://img-blog.csdnimg.cn/2021013018121150.png) +
diff --git a/problems/周总结/20201010二叉树周末总结.md b/problems/周总结/20201010二叉树周末总结.md index d62fa5a5..7b1e7f68 100644 --- a/problems/周总结/20201010二叉树周末总结.md +++ b/problems/周总结/20201010二叉树周末总结.md @@ -87,3 +87,4 @@ **如果大家一路跟下来,一定收获满满,如果周末不做这个总结,大家可能都不知道自己收获满满,啊哈!** +
diff --git a/problems/周总结/20201017二叉树周末总结.md b/problems/周总结/20201017二叉树周末总结.md index e642bfb2..c061a48e 100644 --- a/problems/周总结/20201017二叉树周末总结.md +++ b/problems/周总结/20201017二叉树周末总结.md @@ -116,3 +116,4 @@ 大家如果每天坚持跟下来,会发现又是充实的一周![机智] +
diff --git a/problems/周总结/20201030回溯周末总结.md b/problems/周总结/20201030回溯周末总结.md index cbb0eb8a..54cdc3b5 100644 --- a/problems/周总结/20201030回溯周末总结.md +++ b/problems/周总结/20201030回溯周末总结.md @@ -112,4 +112,4 @@ * B站:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -![](../pics/公众号.png) +
diff --git a/problems/周总结/20201107回溯周末总结.md b/problems/周总结/20201107回溯周末总结.md index 8f2e762d..66036f64 100644 --- a/problems/周总结/20201107回溯周末总结.md +++ b/problems/周总结/20201107回溯周末总结.md @@ -167,3 +167,4 @@ leetcode上的计时应该是以4ms为单位,有的多提交几次,多个4ms +
diff --git a/problems/周总结/20201112回溯周末总结.md b/problems/周总结/20201112回溯周末总结.md index 886b8923..9ca3cff1 100644 --- a/problems/周总结/20201112回溯周末总结.md +++ b/problems/周总结/20201112回溯周末总结.md @@ -95,3 +95,4 @@ +
diff --git a/problems/周总结/20201126贪心周末总结.md b/problems/周总结/20201126贪心周末总结.md index 215e8f01..ea504991 100644 --- a/problems/周总结/20201126贪心周末总结.md +++ b/problems/周总结/20201126贪心周末总结.md @@ -112,3 +112,4 @@ public: +
diff --git a/problems/周总结/20201203贪心周末总结.md b/problems/周总结/20201203贪心周末总结.md index 43e877dd..a82c5d4a 100644 --- a/problems/周总结/20201203贪心周末总结.md +++ b/problems/周总结/20201203贪心周末总结.md @@ -96,3 +96,4 @@ +
diff --git a/problems/周总结/20201210复杂度分析周末总结.md b/problems/周总结/20201210复杂度分析周末总结.md index 1833c1ad..8fe13f0b 100644 --- a/problems/周总结/20201210复杂度分析周末总结.md +++ b/problems/周总结/20201210复杂度分析周末总结.md @@ -119,3 +119,4 @@ 就酱,「代码随想录」是技术公众号里的一抹清流,值得推荐给身边的朋友同学们! +
diff --git a/problems/周总结/20201217贪心周末总结.md b/problems/周总结/20201217贪心周末总结.md index 4a634da5..4363027c 100644 --- a/problems/周总结/20201217贪心周末总结.md +++ b/problems/周总结/20201217贪心周末总结.md @@ -97,3 +97,4 @@ 而且大家也会发现,贪心并没有想象中的那么简单,贪心往往妙的出其不意,触不及防!哈哈 +
diff --git a/problems/周总结/20201224贪心周末总结.md b/problems/周总结/20201224贪心周末总结.md index cdc62168..a82433cf 100644 --- a/problems/周总结/20201224贪心周末总结.md +++ b/problems/周总结/20201224贪心周末总结.md @@ -31,7 +31,7 @@ 把[贪心算法:用最少数量的箭引爆气球](https://mp.weixin.qq.com/s/HxVAJ6INMfNKiGwI88-RFw)代码稍做修改,别可以AC本题。 修改后的C++代码如下: -```C++ +```CPP class Solution { public: // 按照区间左边界从大到小排序 @@ -102,3 +102,4 @@ public: **「代码随想录」里总结的都是经典题目,大家跟着练就节省了不少选择题目的时间了**。 +
diff --git a/problems/周总结/20210107动规周末总结.md b/problems/周总结/20210107动规周末总结.md index 24700941..67957f05 100644 --- a/problems/周总结/20210107动规周末总结.md +++ b/problems/周总结/20210107动规周末总结.md @@ -51,7 +51,7 @@ dp[0]其实就是一个无意义的存在,不用去初始化dp[0]。 一个严谨的思考过程,应该是初始化dp[1] = 1,dp[2] = 2,然后i从3开始遍历,代码如下: -```C++ +```CPP dp[1] = 1; dp[2] = 2; for (int i = 3; i <= n; i++) { // 注意i是从3开始的 @@ -67,7 +67,7 @@ for (int i = 3; i <= n; i++) { // 注意i是从3开始的 这里我先给出我的实现代码: -```C++ +```CPP class Solution { public: int climbStairs(int n) { @@ -122,7 +122,7 @@ public: 所以代码也可以这么写: -```C++ +```CPP class Solution { public: int minCostClimbingStairs(vector& cost) { @@ -149,3 +149,4 @@ public: +
diff --git a/problems/周总结/20210114动规周末总结.md b/problems/周总结/20210114动规周末总结.md index acce0fb2..a49e0370 100644 --- a/problems/周总结/20210114动规周末总结.md +++ b/problems/周总结/20210114动规周末总结.md @@ -157,3 +157,4 @@ n为5时候的dp数组状态如图: 预告,我们下周正式开始讲解背包问题,经典的不能再经典,也是比较难的一类动态规划的题目了,录友们上车抓稳咯。 +
diff --git a/problems/周总结/20210121动规周末总结.md b/problems/周总结/20210121动规周末总结.md index dc0e7a46..bffe47fb 100644 --- a/problems/周总结/20210121动规周末总结.md +++ b/problems/周总结/20210121动规周末总结.md @@ -29,7 +29,7 @@ dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); 3. dp数组如何初始化 -```C++ +```CPP // 初始化 dp vector> dp(weight.size() + 1, vector(bagWeight + 1, 0)); for (int j = bagWeight; j >= weight[0]; j--) { @@ -43,7 +43,7 @@ for (int j = bagWeight; j >= weight[0]; j--) { 但是先遍历物品更好理解。代码如下: -```C++ +```CPP // weight数组的大小 就是物品个数 for(int i = 1; i < weight.size(); i++) { // 遍历物品 for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 @@ -107,7 +107,7 @@ dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); 代码如下: -```C++ +```CPP for(int i = 0; i < weight.size(); i++) { // 遍历物品 for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); @@ -158,3 +158,4 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 就像是我们讲解01背包的时候,花了那么大力气才把每一个细节都讲清楚,这里其实是基础,后面的背包问题怎么变,基础比较牢固自然会有自己的一套思考过程。 +
diff --git a/problems/周总结/20210128动规周末总结.md b/problems/周总结/20210128动规周末总结.md index bd597e41..bea4cc60 100644 --- a/problems/周总结/20210128动规周末总结.md +++ b/problems/周总结/20210128动规周末总结.md @@ -86,7 +86,7 @@ dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1); 完全背包的物品是可以添加多次的,所以遍历背包容量要从小到大去遍历,即: -```C++ +```CPP // 先遍历物品,再遍历背包 for(int i = 0; i < weight.size(); i++) { // 遍历物品 for(int j = weight[i]; j < bagWeight ; j++) { // 遍历背包容量 @@ -139,3 +139,4 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 此时相信大家对动规五部曲也有更深的理解了,同样也验证了Carl之前讲过的:**简单题是用来学习方法论的,而遇到难题才体现出方法论的重要性!** +
diff --git a/problems/周总结/20210204动规周末总结.md b/problems/周总结/20210204动规周末总结.md index db14f7f3..d2417f8f 100644 --- a/problems/周总结/20210204动规周末总结.md +++ b/problems/周总结/20210204动规周末总结.md @@ -20,7 +20,7 @@ 所以本题遍历顺序最终遍历顺序:**target(背包)放在外循环,将nums(物品)放在内循环,内循环从前到后遍历**。 -```C++ +```CPP class Solution { public: int combinationSum4(vector& nums, int target) { @@ -56,7 +56,7 @@ public: 和昨天的题目[动态规划:377. 组合总和 Ⅳ](https://mp.weixin.qq.com/s/Iixw0nahJWQgbqVNk8k6gA)基本就是一道题了,遍历顺序也是一样一样的! 代码如下: -```C++ +```CPP class Solution { public: int climbStairs(int n) { @@ -93,7 +93,7 @@ public: 外层for循环遍历物品,内层for遍历背包: -```C++ +```CPP // 版本一 class Solution { public: @@ -115,7 +115,7 @@ public: 外层for遍历背包,内层for循环遍历物品: -```C++ +```CPP // 版本二 class Solution { public: @@ -148,7 +148,7 @@ public: 先遍历背包,在遍历物品: -```C++ +```CPP // 版本一 class Solution { public: @@ -167,7 +167,7 @@ public: 先遍历物品,在遍历背包: -```C++ +```CPP // 版本二 class Solution { public: @@ -200,3 +200,4 @@ public: 此时我们就已经把完全背包的遍历顺序研究的透透的了! +
diff --git a/problems/周总结/20210225动规周末总结.md b/problems/周总结/20210225动规周末总结.md index 739d0469..ea3c5515 100644 --- a/problems/周总结/20210225动规周末总结.md +++ b/problems/周总结/20210225动规周末总结.md @@ -71,7 +71,7 @@ dp[1] = max(nums[0], nums[1]); 这道题目我给出了暴力的解法: -```C++ +```CPP class Solution { public: int rob(TreeNode* root) { @@ -94,7 +94,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: unordered_map umap; // 记录计算过的结果 @@ -120,7 +120,7 @@ public: 1. 确定递归函数的参数和返回值 -```C++ +```CPP vector robTree(TreeNode* cur) { ``` @@ -142,7 +142,7 @@ if (cur == NULL) return vector{0, 0}; 采用后序遍历,代码如下: -```C++ +```CPP // 下标0:不偷,下标1:偷 vector left = robTree(cur->left); // 左 vector right = robTree(cur->right); // 右 @@ -160,7 +160,7 @@ vector right = robTree(cur->right); // 右 代码如下: -```C++ +```CPP vector left = robTree(cur->left); // 左 vector right = robTree(cur->right); // 右 @@ -218,7 +218,7 @@ public: 因为股票就买卖一次,那么贪心的想法很自然就是取最左最小值,取最右最大值,那么得到的差值就是最大利润。 -```C++ +```CPP class Solution { public: int maxProfit(vector& prices) { @@ -237,7 +237,7 @@ public: 动规解法,版本一,代码如下: -```C++ +```CPP // 版本一 class Solution { public: @@ -262,7 +262,7 @@ public: 那么我们只需要记录 当前天的dp状态和前一天的dp状态就可以了,可以使用滚动数组来节省空间,代码如下: -```C++ +```CPP // 版本二 class Solution { public: @@ -300,3 +300,4 @@ public: **代码随想录温馨提醒:投资有风险,入市需谨慎!** +
diff --git a/problems/周总结/20210304动规周末总结.md b/problems/周总结/20210304动规周末总结.md index 977b41e0..9fea6244 100644 --- a/problems/周总结/20210304动规周末总结.md +++ b/problems/周总结/20210304动规周末总结.md @@ -110,7 +110,7 @@ j的状态表示为: 还要强调一下:dp[i][1],**表示的是第i天,买入股票的状态,并不是说一定要第i天买入股票,这是很多同学容易陷入的误区**。 -```C++ +```CPP for (int j = 0; j < 2 * k - 1; j += 2) { dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]); @@ -125,7 +125,7 @@ for (int j = 0; j < 2 * k - 1; j += 2) { 代码如下: -```C++ +```CPP for (int j = 1; j < 2 * k; j += 2) { dp[0][j] = -prices[0]; } @@ -202,3 +202,4 @@ vector> dp(n, vector(3, 0)); 下周还会有一篇股票系列的文章,**股票系列后面我也会单独写一篇总结,来高度概括一下,这样大家会对股票问题就有一个整体性的理解了**。 +
diff --git a/problems/周总结/二叉树阶段总结系列一.md b/problems/周总结/二叉树阶段总结系列一.md index dc73672d..c8235ff1 100644 --- a/problems/周总结/二叉树阶段总结系列一.md +++ b/problems/周总结/二叉树阶段总结系列一.md @@ -18,7 +18,7 @@ 对于二叉树节点的定义,C++代码如下: -```C++ +```CPP struct TreeNode { int val; TreeNode *left; @@ -40,7 +40,7 @@ TreeNode* a = new TreeNode(9); 没有构造函数的话就要这么写: -```C++ +```CPP TreeNode* a = new TreeNode(); a->val = 9; a->left = NULL; @@ -68,7 +68,7 @@ morris遍历是二叉树遍历算法的超强进阶算法,morris遍历可以 前序遍历空节点不入栈的代码:(注意注释部分,和文章中的区别) -```C++ +```CPP class Solution { public: vector preorderTraversal(TreeNode* root) { @@ -91,7 +91,7 @@ public: 后序遍历空节点不入栈的代码:(注意注释部分,和文章中的区别) -```C++ +```CPP class Solution { public: vector postorderTraversal(TreeNode* root) { @@ -152,7 +152,7 @@ public: 如果非要使用递归中序的方式写,也可以,如下代码就可以避免节点左右孩子翻转两次的情况: -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -171,7 +171,7 @@ public: 代码如下: -```C++ +```CPP class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -206,3 +206,4 @@ public: **本周我们都是讲解了二叉树,从理论基础到遍历方式,从递归到迭代,从深度遍历到广度遍历,最后再用了一个翻转二叉树的题目把我们之前讲过的遍历方式都串了起来。** +
diff --git a/problems/哈希表总结.md b/problems/哈希表总结.md index c3fbde2b..28865920 100644 --- a/problems/哈希表总结.md +++ b/problems/哈希表总结.md @@ -84,7 +84,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 来说一说:使用数组和set来做哈希法的局限。 * 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。 -* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下表位置,因为要返回x 和 y的下表。所以set 也不能用。 +* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。 map是一种``的结构,本题可以用key保存数值,用value在保存数值所在的下表。所以使用map最为合适。 @@ -131,4 +131,4 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/哈希表理论基础.md b/problems/哈希表理论基础.md index f78dc241..2d3b03bd 100644 --- a/problems/哈希表理论基础.md +++ b/problems/哈希表理论基础.md @@ -133,4 +133,4 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/回溯总结.md b/problems/回溯总结.md index 793df516..474db74a 100644 --- a/problems/回溯总结.md +++ b/problems/回溯总结.md @@ -454,4 +454,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index 19a4ae3b..cc563fb1 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -30,7 +30,7 @@ used数组去重版本: [回溯算法:求子集问题(二)](https://mp.w 使用set去重的版本如下: -```C++ +```CPP class Solution { private: vector> result; @@ -71,7 +71,7 @@ public: 例如: -```C++ +```CPP class Solution { private: vector> result; @@ -110,7 +110,7 @@ private: 代码如下: -```C++ +```CPP class Solution { private: vector> result; @@ -142,7 +142,7 @@ uset已经是全局变量,本层的uset记录了一个元素,然后进入下 使用set去重的版本如下: -```C++ +```CPP class Solution { private: vector> result; @@ -183,7 +183,7 @@ public: 使用set去重的版本如下: -```C++ +```CPP class Solution { private: vector> result; @@ -263,4 +263,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/回溯算法理论基础.md b/problems/回溯算法理论基础.md index 35e9db0f..b86c2506 100644 --- a/problems/回溯算法理论基础.md +++ b/problems/回溯算法理论基础.md @@ -181,4 +181,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/字符串总结.md b/problems/字符串总结.md index 71be6422..f9c9766e 100644 --- a/problems/字符串总结.md +++ b/problems/字符串总结.md @@ -130,4 +130,4 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/数组总结篇.md b/problems/数组总结篇.md index 2c679493..009cc6f2 100644 --- a/problems/数组总结篇.md +++ b/problems/数组总结篇.md @@ -149,4 +149,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/数组理论基础.md b/problems/数组理论基础.md index 77ecae9d..6d7b9f9a 100644 --- a/problems/数组理论基础.md +++ b/problems/数组理论基础.md @@ -53,7 +53,7 @@ 我们来做一个实验,C++测试代码如下: -```C++ +```CPP void test_arr() { int array[2][3] = { {0, 1, 2}, @@ -124,4 +124,4 @@ public static void test_arr() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/栈与队列总结.md b/problems/栈与队列总结.md index b3bb5c47..70ef7e9c 100644 --- a/problems/栈与队列总结.md +++ b/problems/栈与队列总结.md @@ -181,4 +181,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/栈与队列理论基础.md b/problems/栈与队列理论基础.md index db871a3c..c43ce0f5 100644 --- a/problems/栈与队列理论基础.md +++ b/problems/栈与队列理论基础.md @@ -94,4 +94,4 @@ std::queue> third; // 定义以list为底层容器的队列 * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/根据身高重建队列(vector原理讲解).md b/problems/根据身高重建队列(vector原理讲解).md index c548f6d8..baf7dcf5 100644 --- a/problems/根据身高重建队列(vector原理讲解).md +++ b/problems/根据身高重建队列(vector原理讲解).md @@ -12,7 +12,7 @@ 但是在解释的过程中有不恰当的地方,所以来专门写一篇文章来详细说一说这个问题。 使用vector的代码如下: -```C++ +```CPP // 版本一,使用vector(动态数组) class Solution { public: @@ -38,7 +38,7 @@ public: 其直观上来看数组的insert操作是O(n)的,整体代码的时间复杂度是O(n^2)。 这么一分析好像和版本二链表实现的时间复杂度是一样的啊,为什么提交之后效率会差距这么大呢? -```C++ +```CPP // 版本二,使用list(链表) class Solution { public: @@ -107,7 +107,7 @@ for (int i = 0; i < vec.size(); i++) { 手动模拟的过程其实不是很简单的,需要很多细节,我粗略写了一个版本,如下: -```C++ +```CPP // 版本三 // 使用vector,但不让它动态扩容 class Solution { @@ -180,4 +180,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/算法模板.md b/problems/算法模板.md index 888ae773..8f6b053e 100644 --- a/problems/算法模板.md +++ b/problems/算法模板.md @@ -296,4 +296,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/背包总结篇.md b/problems/背包总结篇.md index 0dd407ac..7ec2d6bf 100644 --- a/problems/背包总结篇.md +++ b/problems/背包总结篇.md @@ -101,4 +101,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 9f09103d..c13efc3a 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -223,7 +223,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 ## 完整C++测试代码 -```C++ +```CPP void test_2_wei_bag_problem1() { vector weight = {1, 3, 4}; vector value = {15, 20, 30}; @@ -434,4 +434,4 @@ test(); * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index 598a7be0..4f1455e9 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -153,7 +153,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 ## 一维dp01背包完整C++测试代码 -```C++ +```CPP void test_1_wei_bag_problem() { vector weight = {1, 3, 4}; vector value = {15, 20, 30}; @@ -323,4 +323,4 @@ test(); * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index e14575d4..85cbf4de 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -56,7 +56,7 @@ 这种方式来实现多重背包的代码如下: -```C++ +```CPP void test_multi_pack() { vector weight = {1, 3, 4}; vector value = {15, 20, 30}; @@ -96,7 +96,7 @@ int main() { 代码如下:(详看注释) -```C++ +```CPP void test_multi_pack() { vector weight = {1, 3, 4}; vector value = {15, 20, 30}; @@ -207,4 +207,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 8a02889a..dc4f06f9 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -51,7 +51,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 而完全背包的物品是可以添加多次的,所以要从小到大去遍历,即: -```C++ +```CPP // 先遍历物品,再遍历背包 for(int i = 0; i < weight.size(); i++) { // 遍历物品 for(int j = weight[i]; j < bagWeight ; j++) { // 遍历背包容量 @@ -98,7 +98,7 @@ dp状态图如下: 先遍历被背包在遍历物品,代码如下: -```C++ +```CPP // 先遍历背包,再遍历物品 for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 for(int i = 0; i < weight.size(); i++) { // 遍历物品 @@ -112,7 +112,7 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 完整的C++测试代码如下: -```C++ +```CPP // 先遍历物品,在遍历背包 void test_CompletePack() { vector weight = {1, 3, 4}; @@ -132,7 +132,7 @@ int main() { ``` -```C++ +```CPP // 先遍历背包,再遍历物品 void test_CompletePack() { @@ -349,4 +349,4 @@ function test_completePack2() { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/贪心算法总结篇.md b/problems/贪心算法总结篇.md index 999797ad..413eb3ac 100644 --- a/problems/贪心算法总结篇.md +++ b/problems/贪心算法总结篇.md @@ -159,4 +159,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/贪心算法理论基础.md b/problems/贪心算法理论基础.md index 5385aa60..77e8fe95 100644 --- a/problems/贪心算法理论基础.md +++ b/problems/贪心算法理论基础.md @@ -108,4 +108,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/链表总结篇.md b/problems/链表总结篇.md index 6404dd6e..1f0de6fb 100644 --- a/problems/链表总结篇.md +++ b/problems/链表总结篇.md @@ -98,4 +98,4 @@ * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 252247c7..d210b6bd 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -159,4 +159,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index c6779427..9acda71c 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -44,7 +44,7 @@ C++代码如下: -```C++ +```CPP class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { @@ -271,4 +271,4 @@ var getIntersectionNode = function(headA, headB) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+