From 4d64b1fe75c43d2cb8ef66ca9dfa52c633b55c2f Mon Sep 17 00:00:00 2001 From: daniel1n <54945782+daniel1n@users.noreply.github.com> Date: Fri, 2 Jul 2021 15:41:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200108.=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新java的 递归和迭代 --- ...将有序数组转换为二叉搜索树.md | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index b8861b24..87b11ed4 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -209,6 +209,8 @@ public: Java: + +递归: 左闭右开 [left,right) ```Java class Solution { public TreeNode sortedArrayToBST(int[] nums) { @@ -232,6 +234,75 @@ class Solution { ``` +递归: 左闭右闭 [left,right] +```java +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + TreeNode root = traversal(nums, 0, nums.length - 1); + return root; + } + + // 左闭右闭区间[left, right) + private TreeNode traversal(int[] nums, int left, int right) { + if (left > right) return null; + + int mid = left + ((right - left) >> 1); + TreeNode root = new TreeNode(nums[mid]); + root.left = traversal(nums, left, mid - 1); + root.right = traversal(nums, mid + 1, right); + return root; + } +} +``` +迭代: 左闭右闭 [left,right] +```java +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + if (nums.length == 0) return null; + + //根节点初始化 + TreeNode root = new TreeNode(-1); + Queue nodeQueue = new LinkedList<>(); + Queue leftQueue = new LinkedList<>(); + Queue rightQueue = new LinkedList<>(); + + // 根节点入队列 + nodeQueue.offer(root); + // 0为左区间下表初始位置 + leftQueue.offer(0); + // nums.size() - 1为右区间下表初始位置 + rightQueue.offer(nums.length - 1); + + while (!nodeQueue.isEmpty()) { + TreeNode currNode = nodeQueue.poll(); + int left = leftQueue.poll(); + int right = rightQueue.poll(); + int mid = left + ((right - left) >> 1); + + // 将mid对应的元素给中间节点 + currNode.val = nums[mid]; + + // 处理左区间 + if (left <= mid - 1) { + currNode.left = new TreeNode(-1); + nodeQueue.offer(currNode.left); + leftQueue.offer(left); + rightQueue.offer(mid - 1); + } + + // 处理右区间 + if (right >= mid + 1) { + currNode.right = new TreeNode(-1); + nodeQueue.offer(currNode.right); + leftQueue.offer(mid + 1); + rightQueue.offer(right); + } + } + return root; + } +} +``` + Python: ```python3 # Definition for a binary tree node. From 6f7a2545b6a52070450b565a136b9dbba586135c Mon Sep 17 00:00:00 2001 From: Jijie LIU Date: Fri, 2 Jul 2021 15:44:32 +0200 Subject: [PATCH 2/3] =?UTF-8?q?update115:=20=E4=BD=BF=E7=94=A8=E4=B8=80?= =?UTF-8?q?=E7=BB=B4dp=E6=95=B0=E7=BB=84=EF=BC=8C=E5=B9=B6=E5=89=AA?= =?UTF-8?q?=E6=9E=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0115.不同的子序列.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index d3bc6d97..62af9d0f 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -186,6 +186,40 @@ class Solution: return dp[-1][-1] ``` +Python3: +```python +class SolutionDP2: + """ + 既然dp[i]只用到dp[i - 1]的状态, + 我们可以通过缓存dp[i - 1]的状态来对dp进行压缩, + 减少空间复杂度。 + (原理等同同于滚动数组) + """ + + def numDistinct(self, s: str, t: str) -> int: + n1, n2 = len(s), len(t) + if n1 < n2: + return 0 + + dp = [0 for _ in range(n2 + 1)] + dp[0] = 1 + + for i in range(1, n1 + 1): + # 必须深拷贝 + # 不然prev[i]和dp[i]是同一个地址的引用 + prev = dp.copy() + # 剪枝,保证s的长度大于等于t + # 因为对于任意i,i > n1, dp[i] = 0 + # 没必要跟新状态。 + end = i if i < n2 else n2 + for j in range(1, end + 1): + if s[i - 1] == t[j - 1]: + dp[j] = prev[j - 1] + prev[j] + else: + dp[j] = prev[j] + return dp[-1] +``` + Go: From abac363863b792ebdd39271ac27badd8ba2bc16b Mon Sep 17 00:00:00 2001 From: liuwei20210312 <1970650352@qq.com> Date: Sat, 3 Jul 2021 19:00:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=EF=BC=8C2020100?= =?UTF-8?q?3=E4=BA=8C=E5=8F=89=E6=A0=91=E5=91=A8=E6=9C=AB=E6=80=BB?= =?UTF-8?q?=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/周总结/20201003二叉树周末总结.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/problems/周总结/20201003二叉树周末总结.md b/problems/周总结/20201003二叉树周末总结.md index b7c123bc..0cb8b654 100644 --- a/problems/周总结/20201003二叉树周末总结.md +++ b/problems/周总结/20201003二叉树周末总结.md @@ -40,9 +40,8 @@ public: return isSame; } - bool isSymmetric(TreeNode* root) { - if (root == NULL) return true; - return compare(root->left, root->right); + bool isSymmetric(TreeNode* p, TreeNode* q) { + return compare(p, q); } }; ```