From a720426f8d3ae143a9e83d823813a8e7b903496f Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 8 Jun 2021 17:59:47 -0700 Subject: [PATCH 01/13] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0392.判断子序列.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index 512ebc82..9048ac44 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -144,7 +144,20 @@ Java: Python: - +```python +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + dp = [[0] * (len(t)+1) for _ in range(len(s)+1)] + for i in range(1, len(s)+1): + for j in range(1, len(t)+1): + if s[i-1] == t[j-1]: + dp[i][j] = dp[i-1][j-1] + 1 + else: + dp[i][j] = dp[i][j-1] + if dp[-1][-1] == len(s): + return True + return False +``` Go: From 426db06f7ad54ce4783774140e19446b26b3b19d Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 8 Jun 2021 20:41:07 -0700 Subject: [PATCH 02/13] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0583.两个字符串的删除操作.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index cd550d65..0b32d129 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -107,7 +107,22 @@ Java: Python: - +```python +class Solution: + def minDistance(self, word1: str, word2: str) -> int: + dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)] + for i in range(len(word1)+1): + dp[i][0] = i + for j in range(len(word2)+1): + dp[0][j] = j + for i in range(1, len(word1)+1): + for j in range(1, len(word2)+1): + if word1[i-1] == word2[j-1]: + dp[i][j] = dp[i-1][j-1] + else: + dp[i][j] = min(dp[i-1][j-1] + 2, dp[i-1][j] + 1, dp[i][j-1] + 1) + return dp[-1][-1] +``` Go: From e42433ee8c69c8b1513dea6141e7449f1107b165 Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 8 Jun 2021 20:47:35 -0700 Subject: [PATCH 03/13] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0072.编辑距离.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 824c74af..2044ac25 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -228,7 +228,22 @@ public int minDistance(String word1, String word2) { ``` Python: - +```python +class Solution: + def minDistance(self, word1: str, word2: str) -> int: + dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)] + for i in range(len(word1)+1): + dp[i][0] = i + for j in range(len(word2)+1): + dp[0][j] = j + for i in range(1, len(word1)+1): + for j in range(1, len(word2)+1): + if word1[i-1] == word2[j-1]: + dp[i][j] = dp[i-1][j-1] + else: + dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1 + return dp[-1][-1] +``` Go: ```Go From 9b3853eae2761011f82b77aa45b840482e1b472b Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 8 Jun 2021 21:05:11 -0700 Subject: [PATCH 04/13] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0647.回文子串.md | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index 58949571..31734bbc 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -284,6 +284,56 @@ class Solution { Python: +> 动态规划: +```python +class Solution: + def countSubstrings(self, s: str) -> int: + dp = [[False] * len(s) for _ in range(len(s))] + result = 0 + for i in range(len(s)-1, -1, -1): #注意遍历顺序 + for j in range(i, len(s)): + if s[i] == s[j]: + if j - i <= 1: #情况一 和 情况二 + result += 1 + dp[i][j] = True + elif dp[i+1][j-1]: #情况三 + result += 1 + dp[i][j] = True + return result +``` + +> 动态规划:简洁版 +```python +class Solution: + def countSubstrings(self, s: str) -> int: + dp = [[False] * len(s) for _ in range(len(s))] + result = 0 + for i in range(len(s)-1, -1, -1): #注意遍历顺序 + for j in range(i, len(s)): + if s[i] == s[j] and (j - i <= 1 or dp[i+1][j-1]): + result += 1 + dp[i][j] = True + return result +``` + +> 双指针法: +```python +class Solution: + def countSubstrings(self, s: str) -> int: + result = 0 + for i in range(len(s)): + result += self.extend(s, i, i, len(s)) #以i为中心 + result += self.extend(s, i, i+1, len(s)) #以i和i+1为中心 + return result + + def extend(self, s, i, j, n): + res = 0 + while i >= 0 and j < n and s[i] == s[j]: + i -= 1 + j += 1 + res += 1 + return res +``` Go: ```Go From 0e25b3ce32f0f62432b0d39a1b9c67713e8d2f42 Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 8 Jun 2021 21:14:40 -0700 Subject: [PATCH 05/13] =?UTF-8?q?Update=200516.=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=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 --- problems/0516.最长回文子序列.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index a7dfa648..a4f0522f 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -170,7 +170,20 @@ public class Solution { Python: - +```python +class Solution: + def longestPalindromeSubseq(self, s: str) -> int: + dp = [[0] * len(s) for _ in range(len(s))] + for i in range(len(s)): + dp[i][i] = 1 + for i in range(len(s)-1, -1, -1): + for j in range(i+1, len(s)): + if s[i] == s[j]: + dp[i][j] = dp[i+1][j-1] + 2 + else: + dp[i][j] = max(dp[i+1][j], dp[i][j-1]) + return dp[0][-1] +``` Go: ```Go From 7343bb7b14cbd9ae3fc8af2c705f4f98f56f7dad Mon Sep 17 00:00:00 2001 From: SkyLazy <627770537@qq.com> Date: Wed, 9 Jun 2021 16:51:32 +0800 Subject: [PATCH 06/13] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit java代码 --- problems/0977.有序数组的平方.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index b5d392e6..f3e9cf38 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -98,6 +98,26 @@ public: Java: +```Java +class Solution { + public int[] sortedSquares(int[] nums) { + int right = nums.length - 1; + int left = 0; + int[] result = new int[nums.length]; + int index = result.length - 1; + while (left <= right) { + if (nums[left] * nums[left] > nums[right] * nums[right]) { + result[index--] = nums[left] * nums[left]; + ++left; + } else { + result[index--] = nums[right] * nums[right]; + --right; + } + } + return result; + } +} +``` Python: ```Python From 49b82575b40e9beff1d19c6995962ac3465da3fc Mon Sep 17 00:00:00 2001 From: haofeng <852172305@qq.com> Date: Wed, 9 Jun 2021 23:13:21 +0800 Subject: [PATCH 07/13] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC.md=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20pyhthon3=20=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯完全背包版本.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 69750f8f..2896ca88 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -147,6 +147,23 @@ class Solution { Python: +```python3 +class Solution: + def climbStairs(self, n: int) -> int: + dp = [0]*(n + 1) + dp[0] = 1 + m = 2 + # 遍历背包 + for j in range(n + 1): + # 遍历物品 + for step in range(1, m + 1): + if j >= step: + dp[j] += dp[j - step] + return dp[n] +``` + + + Go: From 33dcb8dbd3d98ad38bdb37e0d7b6a6113ded4422 Mon Sep 17 00:00:00 2001 From: Yang Date: Wed, 9 Jun 2021 15:46:57 -0400 Subject: [PATCH 08/13] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java code: initialize dp array with length `nums.length` is sufficient --- problems/0198.打家劫舍.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index f49857bb..63a68c36 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -118,7 +118,7 @@ class Solution { if (nums == null || nums.length == 0) return 0; if (nums.length == 1) return nums[0]; - int[] dp = new int[nums.length + 1]; + int[] dp = new int[nums.length]; dp[0] = nums[0]; dp[1] = Math.max(dp[0], nums[1]); for (int i = 2; i < nums.length; i++) { From 9f3990a78f7ca4719fdb2cb11f6c6a0e203c3a39 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Wed, 9 Jun 2021 23:08:35 +0200 Subject: [PATCH 09/13] =?UTF-8?q?=E4=BC=98=E5=8C=96=201005.K=E6=AC=A1?= =?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 1005.K次取反后最大化的数组和 python3版本 --- problems/1005.K次取反后最大化的数组和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 5539aff8..387de147 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -133,7 +133,7 @@ class Solution: A[i] *= -1 K -= 1 if K > 0: - A[len(A) - 1] *= ((-1)**K) + A[-1] *= (-1)**K #取A最后一个数只需要写-1 return sum(A) ``` From c06f5d2ccefa01488ab457bfe0d370cac015af84 Mon Sep 17 00:00:00 2001 From: Huairuo Yang Date: Wed, 9 Jun 2021 20:08:36 -0400 Subject: [PATCH 10/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC=E5=92=8C=E6=97=A0=E7=94=A8=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove fault and redundant bracket --- problems/0151.翻转字符串里的单词.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 63499b71..d76734e4 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -183,7 +183,7 @@ public: int end = 0; // 反转的单词在字符串里终止位置 bool entry = false; // 标记枚举字符串的过程中是否已经进入了单词区间 for (int i = 0; i < s.size(); i++) { // 开始反转单词 - if ((!entry))) { + if (!entry) { start = i; // 确定单词起始位置 entry = true; // 进入单词区间 } @@ -380,4 +380,4 @@ func reverse(b *[]byte, left, right int) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
From ed7e252e14ce9d2ba151d8080d7098e428350233 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Thu, 10 Jun 2021 11:21:03 +0800 Subject: [PATCH 11/13] =?UTF-8?q?feat(977):=20=E6=96=B0=E5=A2=9Ejava?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index b5d392e6..d28a9427 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -96,9 +96,27 @@ public: ## 其他语言版本 - Java: +```java +class Solution { + public int[] sortedSquares(int[] nums) { + int l = 0; + int r = nums.length - 1; + int[] res = new int[nums.length]; + int j = nums.length - 1; + while(l <= r){ + if(nums[l] * nums[l] > nums[r] * nums[r]){ + res[j--] = nums[l] * nums[l++]; + }else{ + res[j--] = nums[r] * nums[r--]; + } + } + return res; + } +} +``` + Python: ```Python class Solution: From e735356e6dfc8ed17463313941b5dfc2bf37dd7b Mon Sep 17 00:00:00 2001 From: zhangzw Date: Thu, 10 Jun 2021 13:49:22 +0800 Subject: [PATCH 12/13] =?UTF-8?q?update0322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0322.零钱兑换.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 2c9ff8cf..cc288593 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -226,11 +226,11 @@ func coinChange1(coins []int, amount int) int { for i := 0; i < len(coins); i++ { // 遍历背包 for j := coins[i]; j <= amount; j++ { - //if dp[j-coins[i]] != math.MaxInt32 { + if dp[j-coins[i]] != math.MaxInt32 { // 推导公式 dp[j] = min(dp[j], dp[j-coins[i]]+1) - fmt.Println(dp,j,i) - //} + //fmt.Println(dp,j,i) + } } } // 没找到能装满背包的, 就返回-1 From da8f3c92a447ec924b9f9ae5bc182ff2681bc51b Mon Sep 17 00:00:00 2001 From: lichuanming <1465802096@qq.com> Date: Thu, 10 Jun 2021 16:30:39 +0800 Subject: [PATCH 13/13] =?UTF-8?q?0739=20=E5=8D=95=E8=B0=83=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 516d068d..1f91e42a 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -180,7 +180,36 @@ public: Java: - +```java +/** + * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑 + *

+ * 入站元素要和当前栈内栈首元素进行比较 + * 若大于栈首则 则与元素下标做差 + * 若大于等于则放入 + * + * @param temperatures + * @return + */ + public static int[] dailyTemperatures(int[] temperatures) { + Stack stack = new Stack<>(); + int[] res = new int[temperatures.length]; + for (int i = 0; i < temperatures.length; i++) { + /** + * 取出下标进行元素值的比较 + */ + while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { + int preIndex = stack.pop(); + res[preIndex] = i - preIndex; + } + /** + * 注意 放入的是元素位置 + */ + stack.push(i); + } + return res; + } +``` Python: Go: