From fc5b272bd604dfe409a5c699d8253fde515b83ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=97=E8=B1=AA?= Date: Tue, 1 Mar 2022 08:56:51 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix:=200053.=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=20js=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和(动态规划).md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md index 37de9bbe..703e1dd6 100644 --- a/problems/0053.最大子序和(动态规划).md +++ b/problems/0053.最大子序和(动态规划).md @@ -174,6 +174,7 @@ const maxSubArray = nums => { // 数组长度,dp初始化 const len = nums.length; let dp = new Array(len).fill(0); + dp[0] = nums[0]; // 最大值初始化为dp[0] let max = dp[0]; for (let i = 1; i < len; i++) { From 66c3aab86a1c979ca14244c65ee2564be6e288d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8F=82=E7=8F=82?= Date: Tue, 1 Mar 2022 11:27:42 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=AF=B9=E4=BA=8E09=E8=BF=99=E6=A0=B7?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD=E5=8F=AF=E4=BB=A5=E4=B8=8D=E5=81=9A?= =?UTF-8?q?=EF=BC=8C=E7=9B=B4=E6=8E=A5=E7=94=A8String.valueOf=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=B0=B1=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0738.单调递增的数字.md | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index e670bb31..c8ce8a2b 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -148,23 +148,19 @@ java版本1中创建了String数组,多次使用Integer.parseInt了方法, 版本2 class Solution { public int monotoneIncreasingDigits(int n) { - if (n==0)return 0; - char[] chars= Integer.toString(n).toCharArray(); - int start=Integer.MAX_VALUE;//start初始值设为最大值,这是为了防止当数字本身是单调递增时,没有一位数字需要改成9的情况 - for (int i=chars.length-1;i>0;i--){ - if (chars[i]= 0; i--) { + if (chars[i] > chars[i + 1]) { + chars[i]--; + start = i+1; } } - StringBuilder res=new StringBuilder(); - for (int i=0;i=start){ - res.append('9'); - }else res.append(chars[i]); + for (int i = start; i < s.length(); i++) { + chars[i] = '9'; } - return Integer.parseInt(res.toString()); + return Integer.parseInt(String.valueOf(chars)); } } ``` From e67bf9d4330aa3c7cf08f899aef2b11b3bba8cb8 Mon Sep 17 00:00:00 2001 From: Dawn-private <404232992@qq.com> Date: Tue, 1 Mar 2022 19:51:18 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF=E4=B8=8A?= =?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改300.最长递增子序列动态数组的定义 --- problems/0300.最长上升子序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index ed61a30e..dfdd5125 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -37,7 +37,7 @@ 1. dp[i]的定义 -**dp[i]表示i之前包括i的最长上升子序列的长度**。 +**dp[i]表示i之前包括i的以nums[i]结尾最长上升子序列的长度** 2. 状态转移方程 From 10c49296ff3b7d9d97fd1d061beb8db77e771be9 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 1 Mar 2022 21:28:59 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880236.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0236.二叉树的最近公共祖先.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 6213aeaa..ca5fba77 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -325,6 +325,20 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` +## TypeScript + +```typescript +function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { + if (root === null || root === p || root === q) return root; + const left = lowestCommonAncestor(root.left, p, q); + const right = lowestCommonAncestor(root.right, p, q); + if (left !== null && right !== null) return root; + if (left !== null) return left; + if (right !== null) return right; + return null; +}; +``` + ----------------------- From d4e3da4c3db7729441397b7555b5a26b62fb8e2c Mon Sep 17 00:00:00 2001 From: Qiyu Liang <61932152+Guicai996@users.noreply.github.com> Date: Tue, 1 Mar 2022 23:33:54 +0800 Subject: [PATCH 5/5] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D=E6=8B=86?= =?UTF-8?q?=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除原Line 102,修改memory数组为bool型 因为根据执行顺序,Line 101的if判断句,只有在前一个判断返回true的时候才会递归,因此若执行到memory[startIndex] = 1时,程序已经完成了遍历,memory[startIndex] = 1的情况完全没用的上。而memory用上的情况为false重复,即程序已经判断过startIndex开头无法分割。 --- problems/0139.单词拆分.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 1653a81a..e04cb173 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -89,27 +89,26 @@ class Solution { private: bool backtracking (const string& s, const unordered_set& wordSet, - vector& memory, + vector& memory, int startIndex) { if (startIndex >= s.size()) { return true; } // 如果memory[startIndex]不是初始值了,直接使用memory[startIndex]的结果 - if (memory[startIndex] != -1) return memory[startIndex]; + if (!memory[startIndex]) return memory[startIndex]; for (int i = startIndex; i < s.size(); i++) { string word = s.substr(startIndex, i - startIndex + 1); if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, memory, i + 1)) { - memory[startIndex] = 1; // 记录以startIndex开始的子串是可以被拆分的 return true; } } - memory[startIndex] = 0; // 记录以startIndex开始的子串是不可以被拆分的 + memory[startIndex] = false; // 记录以startIndex开始的子串是不可以被拆分的 return false; } public: bool wordBreak(string s, vector& wordDict) { unordered_set wordSet(wordDict.begin(), wordDict.end()); - vector memory(s.size(), -1); // -1 表示初始化状态 + vector memory(s.size(), 1); // -1 表示初始化状态 return backtracking(s, wordSet, memory, 0); } };