From fed1fffac571444cdbf5868ef986a4d627e577ce Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Fri, 27 Jan 2023 09:40:18 -0700 Subject: [PATCH 01/10] =?UTF-8?q?Update=200206.=E7=BF=BB=E8=BD=AC=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python递归法从后向前 --- problems/0206.翻转链表.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 7db80fe1..d29090d9 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -228,7 +228,22 @@ class Solution: ``` +Python递归法从后向前: +```python +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head or not head.next: return head + p = self.reverseList(head.next) + head.next.next = head + head.next = None + return p +``` Go: From d704c10869eb0942af75fb3567c9716c2c4d8696 Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Sat, 28 Jan 2023 10:35:20 +0800 Subject: [PATCH 02/10] =?UTF-8?q?Update=20763=E5=88=92=E5=88=86=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=8C=BA=E9=97=B4=20=E6=96=B9=E6=B3=95=E4=BA=8CPython?= =?UTF-8?q?3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0763.划分字母区间.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 65369d25..d3878dc1 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -242,6 +242,40 @@ class Solution: left = i + 1 return result +# 解法二(不相交区间法) +class Solution: + def partitionLabels(self, s: str) -> List[int]: + # 记录每个字母出现的区间 + def getBord(s): + hash = [[-float('inf')] * 2 for _ in range(26)] + for i in range(len(s)): + if hash[ord(s[i]) - ord('a')][0] == -float('inf'): + hash[ord(s[i]) - ord('a')][0] = i + hash[ord(s[i]) - ord('a')][1] = i + # 去除字符串中未出现的字母所占用区间 + hash_filter = [] + for item in hash: + if item[0] != -float('inf'): hash_filter.append(item) + return hash_filter + + # 得到无重叠区间题意中的输入样例格式:区间列表 + hash = getBord(s) + # 按照左边界从小到大排序 + hash.sort(key= lambda x: x[0]) + res = [] + left = 0 + # 记录最大右边界 + right = hash[0][1] + for i in range(len(hash)): + # 一旦下一区间左边界大于当前右边界,即可认为出现分割点 + if hash[i][0] > right: + res.append(right - left + 1) + left = hash[i][0] + # 实时更新最大右边界 + right = max(right, hash[i][1]) + # 最右侧区间(字符串长度为1时的特殊情况也包含于其中) + res.append(right - left + 1) + return res ``` ### Go From d555e8815a7406649ea680f45fcfadfe0bd4c84e Mon Sep 17 00:00:00 2001 From: YuhanSun Date: Sat, 28 Jan 2023 10:46:03 -0800 Subject: [PATCH 03/10] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 936ba270..952d4274 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -87,7 +87,7 @@ 有录友可能想,那还有装不满的时候? -拿输入数组 [1, 5, 11, 5],距离, dp[7] 只能等于 6,因为 只能放进 1 和 5。 +拿输入数组 [1, 5, 11, 5],举例, dp[7] 只能等于 6,因为 只能放进 1 和 5。 而dp[6] 就可以等于6了,放进1 和 5,那么dp[6] == 6,说明背包装满了。 From 4b396e1f37035796e5407db69d3aa7d74c8ba21b Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sat, 28 Jan 2023 14:27:57 -0600 Subject: [PATCH 04/10] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0322.零钱兑换.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 45974112..7505f5ec 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -209,19 +209,19 @@ class Solution: def coinChange(self, coins: List[int], amount: int) -> int: '''版本一''' # 初始化 - dp = [amount + 1]*(amount + 1) + dp = [float("inf")]*(amount + 1) dp[0] = 0 # 遍历物品 for coin in coins: # 遍历背包 for j in range(coin, amount + 1): dp[j] = min(dp[j], dp[j - coin] + 1) - return dp[amount] if dp[amount] < amount + 1 else -1 + return dp[amount] if dp[amount] != float("inf") else -1 def coinChange1(self, coins: List[int], amount: int) -> int: '''版本二''' # 初始化 - dp = [amount + 1]*(amount + 1) + dp = [float("inf")]*(amount + 1) dp[0] = 0 # 遍历物品 for j in range(1, amount + 1): @@ -229,7 +229,7 @@ class Solution: for coin in coins: if j >= coin: dp[j] = min(dp[j], dp[j - coin] + 1) - return dp[amount] if dp[amount] < amount + 1 else -1 + return dp[amount] if dp[amount] != float("inf") else -1 ``` From 614653f6d86a2edc57375f60a993a626e11290fe Mon Sep 17 00:00:00 2001 From: El nino <69737612+el-nino2020@users.noreply.github.com> Date: Mon, 30 Jan 2023 10:46:26 +0800 Subject: [PATCH 05/10] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF=E8=BF=9E?= =?UTF-8?q?=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typo --- problems/0674.最长连续递增序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 81155f9b..3cb68060 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -52,7 +52,7 @@ **注意这里就体现出和[动态规划:300.最长递增子序列](https://programmercarl.com/0300.最长上升子序列.html)的区别!** -因为本题要求连续递增子序列,所以就必要比较nums[i]与nums[i - 1],而不用去比较nums[j]与nums[i] (j是在0到i之间遍历)。 +因为本题要求连续递增子序列,所以就只要比较nums[i]与nums[i - 1],而不用去比较nums[j]与nums[i] (j是在0到i之间遍历)。 既然不用j了,那么也不用两层for循环,本题一层for循环就行,比较nums[i] 和 nums[i - 1]。 From a4e22fb2d61411dfdccd27995079bd92f3396e96 Mon Sep 17 00:00:00 2001 From: ShuBo6 <814183583@qq.com> Date: Mon, 30 Jan 2023 22:29:45 +0800 Subject: [PATCH 06/10] =?UTF-8?q?bug&&typo:=200063=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II,0343=E6=95=B4=E6=95=B0=E6=8B=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.bug: 0063不同路径II go版本代码 2. typo:0343整数拆分 --- problems/0063.不同路径II.md | 6 ++++-- problems/0343.整数拆分.md | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 9aa36956..96873cfd 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -335,6 +335,10 @@ class Solution: ```go func uniquePathsWithObstacles(obstacleGrid [][]int) int { m, n := len(obstacleGrid), len(obstacleGrid[0]) + //如果在起点或终点出现了障碍,直接返回0 + if obstacleGrid[m-1][n-1] == 1 || obstacleGrid[0][0] == 1 { + return 0 + } // 定义一个dp数组 dp := make([][]int, m) for i, _ := range dp { @@ -359,8 +363,6 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int { } return dp[m-1][n-1] } - - ``` ### Javascript diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 25a63154..78c76cbd 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -88,7 +88,7 @@ dp[i] 是依靠 dp[i - j]的状态,所以遍历i一定是从前向后遍历, 所以遍历顺序为: ```CPP for (int i = 3; i <= n ; i++) { - for (int j = 1; j <= i / 2; j++) { + for (int j = 1; j < i - 1; j++) { dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j)); } } From 7bf3645bde75d74b1126da1e9866647763fff3e1 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 30 Jan 2023 20:02:15 -0600 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index 816719c3..3c5b45c1 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -139,7 +139,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 倒序遍历的原因是,本质上还是一个对二维数组的遍历,并且右下角的值依赖上一层左上角的值,因此需要保证左边的值仍然是上一层的,从右向左覆盖。 -(这里如果读不懂,就在回想一下dp[j]的定义,或者就把两个for循环顺序颠倒一下试试!) +(这里如果读不懂,就再回想一下dp[j]的定义,或者就把两个for循环顺序颠倒一下试试!) **所以一维dp数组的背包在遍历顺序上和二维其实是有很大差异的!**,这一点大家一定要注意。 From 4c08579bd5dd81916768ce3672e1cb1b40049e48 Mon Sep 17 00:00:00 2001 From: simple <37768049+Civitasv@users.noreply.github.com> Date: Tue, 31 Jan 2023 15:04:52 +0800 Subject: [PATCH 08/10] =?UTF-8?q?fix:=200001.=20=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=EF=BC=8Ctypo=EF=BC=9A=E4=B8=8B=E8=A1=A8=3D>?= =?UTF-8?q?=E4=B8=8B=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 56cff527..aa8724db 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -69,7 +69,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 * **map用来做什么** * **map中key和value分别表示什么** -map目的用来存放我们访问过的元素,因为遍历数组的时候,需要记录我们之前遍历过哪些元素和对应的下表,这样才能找到与当前元素相匹配的(也就是相加等于target) +map目的用来存放我们访问过的元素,因为遍历数组的时候,需要记录我们之前遍历过哪些元素和对应的下标,这样才能找到与当前元素相匹配的(也就是相加等于target) 接下来是map中key和value分别表示什么。 @@ -77,7 +77,7 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候, 那么判断元素是否出现,这个元素就要作为key,所以数组中的元素作为key,有key对应的就是value,value用来存下标。 -所以 map中的存储结构为 {key:数据元素,value:数组元素对应的下表}。 +所以 map中的存储结构为 {key:数据元素,value:数组元素对应的下标}。 在遍历数组的时候,只需要向map去查询是否有和目前遍历元素比配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。 From 3347033a2ece87d39f398265f2dbe2d66864c1f8 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Wed, 1 Feb 2023 12:51:07 -0600 Subject: [PATCH 09/10] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E8=AE=B2=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0123.买卖股票的最佳时机III.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index d480da83..3cd7ab26 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -120,7 +120,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 大家可以看到红色框为最后两次卖出的状态。 -现在最大的时候一定是卖出的状态,而两次卖出的状态现金最大一定是最后一次卖出。 +现在最大的时候一定是卖出的状态,而两次卖出的状态现金最大一定是最后一次卖出。如果想不明白的录友也可以这么理解:如果第一次卖出已经是最大值了,那么我们可以在当天立刻买入再立刻卖出。所以dp[4][4]已经包含了dp[4][2]的情况。也就是说第二次卖出手里所剩的钱一定是最多的。 所以最终最大利润是dp[4][4] From 5cf52116e2d20018385050d519d78d78a91e6059 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sun, 5 Feb 2023 01:33:49 -0500 Subject: [PATCH 10/10] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入python-动态规划做法代码 --- problems/0045.跳跃游戏II.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 7dbf531b..d4f2e6ea 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -229,7 +229,19 @@ class Solution: step += 1 return step ``` +```python +# 动态规划做法 +class Solution: + def jump(self, nums: List[int]) -> int: + result = [10**4+1]*len(nums) + result[0]=0 + for i in range(len(nums)): + for j in range(nums[i]+1): + if i+j