diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 90c9a8c0..5c8270b6 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -146,19 +146,24 @@ class Solution { ``` Python: -```python + + +```python3 class Solution: def climbStairs(self, n: int) -> int: - m = 2 - dp = [0] * (n + 1) + dp = [0]*(n + 1) dp[0] = 1 - for i in range(n + 1): - for j in range(1, m + 1): - if i >= j: - dp[i] += dp[i - j] - return dp[-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: ```go func climbStairs(n int) int { 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 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 +
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++) { 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 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: 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 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: 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 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: diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index b5d392e6..d2e1f950 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -96,8 +96,46 @@ 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; + } +} +``` + +```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 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) ```