From c41ce18dbbac14336d49f006c3d864f0b3a232f9 Mon Sep 17 00:00:00 2001 From: Wen Date: Fri, 17 Sep 2021 20:21:56 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200236.=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=20Python3=E8=A7=A3=E6=B3=95=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DPython=E8=AF=AD=E6=B3=95=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0236.二叉树的最近公共祖先.md | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 59345a24..46dcb545 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -264,16 +264,21 @@ class Solution { ## Python ```python -//递归 class Solution: + """二叉树的最近公共祖先 递归法""" + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': - if not root or root == p or root == q: return root //找到了节点p或者q,或者遇到空节点 - left = self.lowestCommonAncestor(root.left,p,q) //左 - right = self.lowestCommonAncestor(root.right,p,q) //右 - if left and right: return root //中: left和right不为空,root就是最近公共节点 - elif left and not right: return left //目标节点是通过left返回的 - elif not left and right: return right //目标节点是通过right返回的 - else: return None //没找到 + if not root or root == p or root == q: + return root + + left = self.lowestCommonAncestor(root.left, p, q) + right = self.lowestCommonAncestor(root.right, p, q) + + if left and right: + return root + if left: + return left + return right ``` ## Go From ac8ca62309bb2dddb9990b8fdb90f23bf3959129 Mon Sep 17 00:00:00 2001 From: Jerry-306 <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 17 Sep 2021 21:43:52 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200739=20=E6=AF=8F?= =?UTF-8?q?=E6=97=A5=E6=B8=A9=E5=BA=A6=20=20JavaScript=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=20=E5=8D=95=E8=B0=83=E6=A0=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index b00701ed..53ce1133 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -276,6 +276,36 @@ func dailyTemperatures(num []int) []int { } ``` +JavaScript: +```javascript +/** + * @param {number[]} temperatures + * @return {number[]} + */ +var dailyTemperatures = function(temperatures) { + let n = temperatures.length; + let res = new Array(n).fill(0); + let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标 + stack.push(0); + for (let i = 1; i < n; i++) { + // 栈顶元素 + let top = stack[stack.length - 1]; + if (temperatures[i] < temperatures[top]) { + stack.push(i); + } else if (temperatures[i] === temperatures[top]) { + stack.push(i); + } else { + while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) { + let top = stack.pop(); + res[top] = i - top; + } + stack.push(i); + } + } + return res; +}; +``` + ----------------------- From 8a4b3b1636af1272e561db534f9a4ccbcec7e45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=BB=B5=E7=A8=8B?= Date: Fri, 17 Sep 2021 16:12:24 -0700 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A00077.=E7=BB=84=E5=90=88py?= =?UTF-8?q?thon2=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 9b44b572..c72adb83 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -368,6 +368,34 @@ class Solution { } ``` +Python2: +```python +class Solution(object): + def combine(self, n, k): + """ + :type n: int + :type k: int + :rtype: List[List[int]] + """ + result = [] + path = [] + def backtracking(n, k, startidx): + if len(path) == k: + result.append(path[:]) + return + + # 剪枝, 最后k - len(path)个节点直接构造结果,无需递归 + last_startidx = n - (k - len(path)) + 1 + result.append(path + [idx for idx in range(last_startidx, n + 1)]) + + for x in range(startidx, last_startidx): + path.append(x) + backtracking(n, k, x + 1) # 递归 + path.pop() # 回溯 + + backtracking(n, k, 1) + return result +``` Python: ```python3 From 99e26666ee48de0d129ff7e85ee3ba248ad7bd94 Mon Sep 17 00:00:00 2001 From: konng Date: Sat, 18 Sep 2021 13:30:25 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E7=BA=A0=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除多余的一个 “这” 字 --- problems/0101.对称二叉树.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 03d3acaa..3abf26cb 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -185,7 +185,8 @@ public: queue que; que.push(root->left); // 将左子树头结点加入队列 que.push(root->right); // 将右子树头结点加入队列 - while (!que.empty()) { // 接下来就要判断这这两个树是否相互翻转 + + while (!que.empty()) { // 接下来就要判断这两个树是否相互翻转 TreeNode* leftNode = que.front(); que.pop(); TreeNode* rightNode = que.front(); que.pop(); if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的 From 43f30b23ab2928f277793bfede1df028b4dd4cfb Mon Sep 17 00:00:00 2001 From: Wen Date: Sat, 18 Sep 2021 22:17:47 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200235.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88.md=20Python3=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0235.二叉搜索树的最近公共祖先.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 929e6eb2..7fad5af0 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -260,13 +260,15 @@ class Solution { 递归法: ```python class Solution: + """二叉搜索树的最近公共祖先 递归法""" + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': - if not root: return root //中 - if root.val >p.val and root.val > q.val: - return self.lowestCommonAncestor(root.left,p,q) //左 - elif root.val < p.val and root.val < q.val: - return self.lowestCommonAncestor(root.right,p,q) //右 - else: return root + if root.val > p.val and root.val > q.val: + return self.lowestCommonAncestor(root.left, p, q) + if root.val < p.val and root.val < q.val: + return self.lowestCommonAncestor(root.right, p, q) + return root + ``` 迭代法: From 08199d56b5c99f3a23150b99a0cdfd4812fcdeb5 Mon Sep 17 00:00:00 2001 From: Wen Date: Sat, 18 Sep 2021 22:18:30 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200235.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88.md=20Python3=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0235.二叉搜索树的最近公共祖先.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 7fad5af0..c9ef95dd 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -268,11 +268,22 @@ class Solution: if root.val < p.val and root.val < q.val: return self.lowestCommonAncestor(root.right, p, q) return root - ``` 迭代法: +```python +class Solution: + """二叉搜索树的最近公共祖先 迭代法""" + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + while True: + if root.val > p.val and root.val > q.val: + root = root.left + elif root.val < p.val and root.val < q.val: + root = root.right + else: + return root +``` ## Go