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/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 2a76a461..14850dc8 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) -## 思路 +# 思路 **首先要注意是判断左叶子,不是二叉树左侧节点,所以不要上来想着层序遍历。** @@ -119,7 +119,7 @@ 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)中的写法,可以写出一个前序遍历的迭代法。 判断条件都是一样的,代码如下: @@ -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) { //采用层序遍历 diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 34ade48e..65e1d87e 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -96,6 +96,29 @@ public: Java: +```Java +class Solution { + public int[] nextGreaterElements(int[] nums) { + //边界判断 + if(nums == null || nums.length <= 1) { + return new int[]{-1}; + } + 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: @@ -112,3 +135,27 @@ 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/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 27c6e83c..1cb5bfd7 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -7,7 +7,7 @@

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

-## 513.找树左下角的值 +# 513.找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值。 @@ -19,7 +19,7 @@ ![513.找树左下角的值1](https://img-blog.csdnimg.cn/20210204153017586.png) -## 思路 +# 思路 本地要找出树的最后一行找到最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。 @@ -37,7 +37,7 @@ 如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。 -如果对二叉树深度和高度还有点疑惑的话,请看:[110.平衡二叉树](https://mp.weixin.qq.com/s/isUS-0HDYknmC0Rr4R8mww)。 +如果对二叉树深度和高度还有点疑惑的话,请看:[110.平衡二叉树](https://mp.weixin.qq.com/s/7QeWnxaAB66LjFJOs40XKg)。 所以要找深度最大的叶子节点。 @@ -53,7 +53,7 @@ 代码如下: -``` +```C++ int maxLen = INT_MIN; // 全局变量 记录最大深度 int maxleftValue; // 全局变量 最大深度最左节点的数值 void traversal(TreeNode* root, int leftLen) @@ -75,7 +75,7 @@ void traversal(TreeNode* root, int leftLen) 代码如下: -``` +```C++ if (root->left == NULL && root->right == NULL) { if (leftLen > maxLen) { maxLen = leftLen; // 更新最大深度 @@ -168,7 +168,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) ## 迭代法 @@ -177,7 +177,7 @@ public: 只需要记录最后一行第一个节点的数值就可以了。 -如果对层序遍历不了解,看这篇[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog),这篇里也给出了层序遍历的模板,稍作修改就一过刷了这道题了。 +如果对层序遍历不了解,看这篇[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/4-bDKi7SdwfBGRm9FYduiA),这篇里也给出了层序遍历的模板,稍作修改就一过刷了这道题了。 代码如下: @@ -203,20 +203,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 // 递归法 @@ -273,9 +273,9 @@ class Solution { -Python: +## Python -**递归 - 回溯** +递归: ```python class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: @@ -300,7 +300,8 @@ class Solution: __traverse(root, 0) return leftmost_val ``` -**迭代 - 层序遍历** + +迭代 - 层序遍历: ```python class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: @@ -320,19 +321,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 { @@ -364,17 +358,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 @@ -396,8 +382,10 @@ func findBottomLeftValue(root *TreeNode) int { } ``` -JavaScript: -1. 递归版本 +## JavaScript + +递归版本: + ```javascript var findBottomLeftValue = function(root) { //首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可 @@ -419,7 +407,8 @@ var findBottomLeftValue = function(root) { return resNode; }; ``` -2. 层序遍历 +层序遍历: + ```javascript var findBottomLeftValue = function(root) { //考虑层序遍历 记录最后一行的第一个节点