From 146c206ca1b28cdf7526d76c797ee0278b276080 Mon Sep 17 00:00:00 2001 From: Pika-Lee Date: Sun, 13 Aug 2023 23:21:06 -0700 Subject: [PATCH 01/17] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit correct the text fault --- problems/二叉树理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index 184dba60..e2c6d83c 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -150,7 +150,7 @@ 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 -**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用非递归的方式来实现的。 +**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用递归的方式来实现的。 而广度优先遍历的实现一般使用队列来实现,这也是队列先进先出的特点所决定的,因为需要先进先出的结构,才能一层一层的来遍历二叉树。 From 12cc9a310b3ef7aaa0857a73f5eb12084403c9bc Mon Sep 17 00:00:00 2001 From: Pika-Lee Date: Mon, 4 Sep 2023 03:52:43 -0700 Subject: [PATCH 02/17] =?UTF-8?q?Update=200235.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改文中描述,与图片对应(当然也可以修改图片) --- problems/0235.二叉搜索树的最近公共祖先.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 2b8af060..b7e92e4e 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -50,15 +50,15 @@ 因为是有序树,所有 如果 中间节点是 q 和 p 的公共祖先,那么 中节点的数组 一定是在 [p, q]区间的。即 中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。 -那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是q 和 p的公共祖先。 那问题来了,**一定是最近公共祖先吗**? +那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是p 和 q的公共祖先。 那问题来了,**一定是最近公共祖先吗**? -如图,我们从根节点搜索,第一次遇到 cur节点是数值在[p, q]区间中,即 节点5,此时可以说明 p 和 q 一定分别存在于 节点 5的左子树,和右子树中。 +如图,我们从根节点搜索,第一次遇到 cur节点是数值在[q, p]区间中,即 节点5,此时可以说明 q 和 p 一定分别存在于 节点 5的左子树,和右子树中。 ![235.二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926164214.png) -此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为q的祖先, 如果从节点5继续向右遍历则错过成为p的祖先。 +此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为p的祖先, 如果从节点5继续向右遍历则错过成为q的祖先。 -所以当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[p, q]区间中,那么cur就是 p和q的最近公共祖先。 +所以当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[q, p]区间中,那么cur就是 q和p的最近公共祖先。 理解这一点,本题就很好解了。 From 9ac418bf220641a1c076f576aed36c02b0f30010 Mon Sep 17 00:00:00 2001 From: Pika-Lee Date: Mon, 4 Sep 2023 03:56:33 -0700 Subject: [PATCH 03/17] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert change of this file --- problems/二叉树理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index e2c6d83c..184dba60 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -150,7 +150,7 @@ 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 -**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用递归的方式来实现的。 +**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用非递归的方式来实现的。 而广度优先遍历的实现一般使用队列来实现,这也是队列先进先出的特点所决定的,因为需要先进先出的结构,才能一层一层的来遍历二叉树。 From 486792cba5260965a82285414425d3a1dab30eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=97=A0=E7=BC=BA?= Date: Wed, 6 Sep 2023 20:46:04 +0800 Subject: [PATCH 04/17] Update acm.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改错别字 --- problems/qita/acm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/qita/acm.md b/problems/qita/acm.md index 1be0e924..99928356 100644 --- a/problems/qita/acm.md +++ b/problems/qita/acm.md @@ -1,5 +1,5 @@ -# 如何练习ACM模式输入输入模式 | 如何准备笔试 | 卡码网 +# 如何练习ACM模式输入输出模式 | 如何准备笔试 | 卡码网 卡码网地址:[https://kamacoder.com](https://kamacoder.com) From 839afd119b4a22700a374f555a0ee8d1eebd9495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=98=94?= Date: Sat, 9 Sep 2023 10:23:43 +0800 Subject: [PATCH 05/17] test --- problems/0111.二叉树的最小深度.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 61f9beb7..8b9d92e6 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -173,12 +173,12 @@ private: int result; void getdepth(TreeNode* node, int depth) { // 函数递归终止条件 - if (root == nullptr) { + if (node == nullptr) { return; } // 中,处理逻辑:判断是不是叶子结点 - if (root -> left == nullptr && root->right == nullptr) { - res = min(res, depth); + if (node -> left == nullptr && node->right == nullptr) { + result = min(result, depth); } if (node->left) { // 左 getdepth(node->left, depth + 1); From 655a513b3145aef9d96db0cdc3983b47572d19cf Mon Sep 17 00:00:00 2001 From: zl127LiamLi <143149260+zl127LiamLi@users.noreply.github.com> Date: Sat, 9 Sep 2023 20:42:36 -0500 Subject: [PATCH 06/17] 841 python3 add BFS solution.md --- problems/0841.钥匙和房间.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index 00156d4e..b4785d1b 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.md @@ -324,7 +324,7 @@ class Solution { ### python3 ```python - +# 深度搜索优先 class Solution: def dfs(self, key: int, rooms: List[List[int]] , visited : List[bool] ) : if visited[key] : @@ -346,6 +346,31 @@ class Solution: return False return True +# 广度搜索优先 +class Solution: + def canVisitAllRooms(self, rooms: List[List[int]]) -> bool: + visited = [False] * len(rooms) + self.bfs(rooms, 0, visited) + + for room in visited: + if room == False: + return False + + return True + + def bfs(self, rooms, index, visited): + q = collections.deque() + q.append(index) + + visited[0] = True + + while len(q) != 0: + index = q.popleft() + for nextIndex in rooms[index]: + if visited[nextIndex] == False: + q.append(nextIndex) + visited[nextIndex] = True + ``` From 4b704d3efd411677309ca6769eb8281560111aa4 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Tue, 12 Sep 2023 20:06:47 +0800 Subject: [PATCH 07/17] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strStr.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0028.实现strStr.md --- problems/0028.实现strStr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 53b57fd5..04fef7fd 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -207,7 +207,7 @@ next数组就是一个前缀表(prefix table)。 ### 前缀表与next数组 -很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? +很多KMP算法的实现都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? next数组就可以是前缀表,但是很多实现都是把前缀表统一减一(右移一位,初始位置为-1)之后作为next数组。 From a0462c4c2539c0b9f65acfc39b32e0bcd9d5a303 Mon Sep 17 00:00:00 2001 From: CH Ye Date: Fri, 15 Sep 2023 16:06:48 -0400 Subject: [PATCH 08/17] =?UTF-8?q?#=20=E6=B7=BB=E5=8A=A0=E4=BA=86=201971.?= =?UTF-8?q?=20=E5=AF=BB=E6=89=BE=E5=9B=BE=E4=B8=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E8=B7=AF=E5=BE=84=20=E7=9A=84Python=E5=B9=B6?= =?UTF-8?q?=E6=9F=A5=E9=9B=86=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1971.寻找图中是否存在路径.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/1971.寻找图中是否存在路径.md b/problems/1971.寻找图中是否存在路径.md index 29e50ab8..5660233c 100644 --- a/problems/1971.寻找图中是否存在路径.md +++ b/problems/1971.寻找图中是否存在路径.md @@ -134,6 +134,22 @@ public: } }; ``` + +PYTHON并查集解法如下: +```PYTHON +class Solution: + def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: + p = [i for i in range(n)] + def find(i): + if p[i] != i: + p[i] = find(p[i]) + return p[i] + for u, v in edges: + p[find(u)] = find(v) + return find(source) == find(destination) +``` + +

From 23ca9e43e5a15bdbbf69744515b94736ac1d3b0d Mon Sep 17 00:00:00 2001 From: CH Ye Date: Fri, 15 Sep 2023 16:09:38 -0400 Subject: [PATCH 09/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86684.=E5=86=97?= =?UTF-8?q?=E4=BD=99=E8=BF=9E=E6=8E=A5=20=E7=9A=84Python=E5=B9=B6=E6=9F=A5?= =?UTF-8?q?=E9=9B=86=E7=AE=80=E6=B4=81=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0684.冗余连接.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md index 8124cc7e..f5e84223 100644 --- a/problems/0684.冗余连接.md +++ b/problems/0684.冗余连接.md @@ -256,6 +256,23 @@ class Solution: return [] ``` +### Python简洁写法: + +```python +class Solution: + def findRedundantConnection(self, edges: List[List[int]]) -> List[int]: + n = len(edges) + p = [i for i in range(n+1)] + def find(i): + if p[i] != i: + p[i] = find(p[i]) + return p[i] + for u, v in edges: + if p[find(u)] == find(v): + return [u, v] + p[find(u)] = find(v) +``` + ### Go ```go From f1a65d7e2df65506a8165cd95ac3aed680cf5a11 Mon Sep 17 00:00:00 2001 From: ShuangmingMa <52561813+ShuangmingMa@users.noreply.github.com> Date: Sat, 16 Sep 2023 18:40:07 +0800 Subject: [PATCH 10/17] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200503.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20go?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0503.下一个更大元素II.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index c001df50..b788968c 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -207,7 +207,7 @@ class Solution: ```go func nextGreaterElements(nums []int) []int { length := len(nums) - result := make([]int,length,length) + result := make([]int,length) for i:=0;i Date: Sat, 16 Sep 2023 20:35:17 +0800 Subject: [PATCH 11/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200042.=E6=8E=A5?= =?UTF-8?q?=E9=9B=A8=E6=B0=B4=20Go=E7=89=88=E6=9C=AC=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E6=A0=88=E5=8E=8B=E7=BC=A9=E7=89=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0042.接雨水.md | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 760ebc34..517ccb0b 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -704,6 +704,45 @@ func min(x, y int) int { } ``` +单调栈压缩版: + +```go +func trap(height []int) int { + stack := make([]int, 0) + res := 0 + + // 无需事先将第一个柱子的坐标入栈,因为它会在该for循环的最后入栈 + for i := 0; i < len(height); i ++ { + // 满足栈不为空并且当前柱子高度大于栈顶对应的柱子高度的情况时 + for len(stack) > 0 && height[stack[len(stack) - 1]] < height[i] { + // 获得凹槽高度 + mid := height[stack[len(stack) - 1]] + // 凹槽坐标出栈 + stack = stack[: len(stack) - 1] + + // 如果栈不为空则此时栈顶元素为左侧柱子坐标 + if len(stack) > 0 { + // 求得雨水高度 + h := min(height[i], height[stack[len(stack) - 1]]) - mid + // 求得雨水宽度 + w := i - stack[len(stack) - 1] - 1 + res += h * w + } + } + // 如果栈为空或者当前柱子高度小于等于栈顶对应的柱子高度时入栈 + stack = append(stack, i) + } + return res +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} +``` + ### JavaScript: ```javascript From c26fddc702a333a3ed36fa10ebbf0f71d617ce7b Mon Sep 17 00:00:00 2001 From: ShuangmingMa <52561813+ShuangmingMa@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:33:02 +0800 Subject: [PATCH 12/17] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200084.=E6=9F=B1?= =?UTF-8?q?=E7=8A=B6=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9?= =?UTF-8?q?=E5=BD=A2=20Go=E5=8D=95=E8=B0=83=E6=A0=88=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0084.柱状图中最大的矩形.md | 56 ++++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index b54429ed..97dab4f3 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -478,36 +478,34 @@ class Solution: ```go func largestRectangleArea(heights []int) int { - // 声明max并初始化为0 - max := 0 - // 使用切片实现栈 - stack := make([]int, 0) - // 数组头部加入0 - heights = append([]int{0}, heights...) - // 数组尾部加入0 - heights = append(heights, 0) - // 初始化栈,序号从0开始 - stack = append(stack, 0) - for i := 1; i < len(heights); i++ { - // 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势 - for heights[stack[len(stack)-1]] > heights[i] { - // mid 是top - mid := stack[len(stack)-1] - // 出栈 - stack = stack[0 : len(stack)-1] - // left是top的下一位元素,i是将要入栈的元素 - left := stack[len(stack)-1] - // 高度x宽度 - tmp := heights[mid] * (i - left - 1) - if tmp > max { - max = tmp - } - } - stack = append(stack, i) - } - return max + max := 0 + // 使用切片实现栈 + stack := make([]int, 0) + // 数组头部加入0 + heights = append([]int{0}, heights...) + // 数组尾部加入0 + heights = append(heights, 0) + // 初始化栈,序号从0开始 + stack = append(stack, 0) + for i := 1; i < len(heights); i ++ { + // 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势 + for heights[stack[len(stack) - 1]] > heights[i] { + // mid 是top + mid := stack[len(stack) - 1] + // 出栈 + stack = stack[0 : len(stack) - 1] + // left是top的下一位元素,i是将要入栈的元素 + left := stack[len(stack) - 1] + // 高度x宽度 + tmp := heights[mid] * (i - left - 1) + if tmp > max { + max = tmp + } + } + stack = append(stack, i) + } + return max } - ``` ### JavaScript: From babb21fad0ac7e9213ef811a1a04415561fd1acf Mon Sep 17 00:00:00 2001 From: km_yang Date: Mon, 18 Sep 2023 14:36:17 +0800 Subject: [PATCH 13/17] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=9A=E2=80=9C=E5=9C=A8=E2=80=9D->=E2=80=9C?= =?UTF-8?q?=E5=86=8D=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/图论广搜理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/图论广搜理论基础.md b/problems/图论广搜理论基础.md index 64ca181b..1174e239 100644 --- a/problems/图论广搜理论基础.md +++ b/problems/图论广搜理论基础.md @@ -9,7 +9,7 @@ 在[深度优先搜索](https://programmercarl.com/图论深搜理论基础.html)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 -广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后在回溯。 +广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后再回溯。 ## 广搜的使用场景 From e14fb76097d117785c0a3af852eaff7d5a174966 Mon Sep 17 00:00:00 2001 From: Junqiao Date: Sun, 24 Sep 2023 10:43:51 +0800 Subject: [PATCH 14/17] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0035.搜索插入位置.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 04dd0cac..80b7e40e 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -258,6 +258,37 @@ public int searchInsert(int[] nums, int target) { +### C# + +```go +public int SearchInsert(int[] nums, int target) { + + var left = 0; + var right = nums.Length - 1; + + while (left <= right) { + + var curr = (left + right) / 2; + + if (nums[curr] == target) + { + return curr; + } + + if (target > nums[curr]) { + left = curr + 1; + } + else { + right = curr - 1; + } + } + + return left; +} +``` + + + ### Golang ```go @@ -500,3 +531,4 @@ int searchInsert(int* nums, int numsSize, int target){ + From 949dd979f7ca88a12a9b364b38e4da2457b27691 Mon Sep 17 00:00:00 2001 From: Junqiao Date: Sun, 24 Sep 2023 11:43:43 +0800 Subject: [PATCH 15/17] =?UTF-8?q?Update=200034.=E5=9C=A8=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...元素的第一个和最后一个位置.md | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index b4128166..3bf90e3b 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -329,6 +329,67 @@ class Solution { } ``` +### C# + +```c# +public int[] SearchRange(int[] nums, int target) { + + var leftBorder = GetLeftBorder(nums, target); + var rightBorder = GetRightBorder(nums, target); + + if (leftBorder == -2 || rightBorder == -2) { + return new int[] {-1, -1}; + } + + if (rightBorder - leftBorder >=2) { + return new int[] {leftBorder + 1, rightBorder - 1}; + } + + return new int[] {-1, -1}; + +} + +public int GetLeftBorder(int[] nums, int target){ + var left = 0; + var right = nums.Length - 1; + var leftBorder = -2; + + while (left <= right) { + var mid = (left + right) / 2; + + if (target <= nums[mid]) { + right = mid - 1; + leftBorder = right; + } + else { + left = mid + 1; + } + } + + return leftBorder; +} + +public int GetRightBorder(int[] nums, int target){ + var left = 0; + var right = nums.Length - 1; + var rightBorder = -2; + + while (left <= right) { + var mid = (left + right) / 2; + + if (target >= nums[mid]) { + left = mid + 1; + rightBorder = left; + } + else { + right = mid - 1; + } + } + + return rightBorder; +} +``` + ### Python From afc9e07d516f7c284163ee71d9b64ed46ab11408 Mon Sep 17 00:00:00 2001 From: Xuan <854674282@qq.com> Date: Mon, 25 Sep 2023 10:21:33 +0800 Subject: [PATCH 16/17] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0=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 新增0343.整数拆分.md PHP 版本 --- problems/0343.整数拆分.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index cba82f6c..2e17caf5 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -469,6 +469,34 @@ object Solution { } ``` + +### PHP +```php +class Solution { + + /** + * @param Integer $n + * @return Integer + */ + function integerBreak($n) { + if($n == 0 || $n == 1) return 0; + if($n == 2) return 1; + + $dp = []; + $dp[0] = 0; + $dp[1] = 0; + $dp[2] = 1; + for($i=3;$i<=$n;$i++){ + for($j = 1;$j <= $i/2; $j++){ + $dp[$i] = max(($i-$j)*$j, $dp[$i-$j]*$j, $dp[$i]); + } + } + + return $dp[$n]; + } +} +``` +

From c2b6be723aab6f2b78fa5a0f3198bf6d37f0edbd Mon Sep 17 00:00:00 2001 From: zirs97 Date: Mon, 25 Sep 2023 22:54:57 +0800 Subject: [PATCH 17/17] =?UTF-8?q?=E4=BF=AE=E6=AD=A30019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84Java=E4=BB=A3=E7=A0=81fastI?= =?UTF-8?q?ndex=E7=A7=BB=E5=8A=A8=E7=9A=84=E9=94=99=E8=AF=AF=E9=83=A8?= =?UTF-8?q?=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0019.删除链表的倒数第N个节点.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 1c95ad5b..dbd053d1 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -105,8 +105,8 @@ public ListNode removeNthFromEnd(ListNode head, int n){ ListNode fastIndex = dummyNode; ListNode slowIndex = dummyNode; - //只要快慢指针相差 n 个结点即可 - for (int i = 0; i < n ; i++){ + // 只要快慢指针相差 n 个结点即可 + for (int i = 0; i <= n ; i++){ fastIndex = fastIndex.next; }