From 0a68e191581e898d6fd5def820c4a4e2b16a896f Mon Sep 17 00:00:00 2001 From: yqq Date: Fri, 27 Aug 2021 19:24:46 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20673.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97=E7=9A=84=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=20=E7=9A=84=20Java,=20Python,=20Go=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0673.最长递增子序列的个数.md | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/problems/0673.最长递增子序列的个数.md b/problems/0673.最长递增子序列的个数.md index ce3e8639..b3907e0e 100644 --- a/problems/0673.最长递增子序列的个数.md +++ b/problems/0673.最长递增子序列的个数.md @@ -9,6 +9,10 @@ # 673.最长递增子序列的个数 + +[力扣题目链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/) + + 给定一个未排序的整数数组,找到最长递增子序列的个数。 示例 1: @@ -224,16 +228,110 @@ public: ## Java ```java +class Solution { + public int findNumberOfLIS(int[] nums) { + if (nums.length <= 1) return nums.length; + int[] dp = new int[nums.length]; + for(int i = 0; i < dp.length; i++) dp[i] = 1; + int[] count = new int[nums.length]; + for(int i = 0; i < count.length; i++) count[i] = 1; + + int maxCount = 0; + for (int i = 1; i < nums.length; i++) { + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + if (dp[j] + 1 > dp[i]) { + dp[i] = dp[j] + 1; + count[i] = count[j]; + } else if (dp[j] + 1 == dp[i]) { + count[i] += count[j]; + } + } + if (dp[i] > maxCount) maxCount = dp[i]; + } + } + int result = 0; + for (int i = 0; i < nums.length; i++) { + if (maxCount == dp[i]) result += count[i]; + } + return result; + } +} ``` ## Python ```python +class Solution: + def findNumberOfLIS(self, nums: List[int]) -> int: + size = len(nums) + if size<= 1: return size + + dp = [1 for i in range(size)] + count = [1 for i in range(size)] + + maxCount = 0 + for i in range(1, size): + for j in range(i): + if nums[i] > nums[j]: + if dp[j] + 1 > dp[i] : + dp[i] = dp[j] + 1 + count[i] = count[j] + elif dp[j] + 1 == dp[i] : + count[i] += count[j] + if dp[i] > maxCount: + maxCount = dp[i]; + result = 0 + for i in range(size): + if maxCount == dp[i]: + result += count[i] + return result; ``` ## Go ```go + +func findNumberOfLIS(nums []int) int { + size := len(nums) + if size <= 1 { + return size + } + + dp := make([]int, size); + for i, _ := range dp { + dp[i] = 1 + } + count := make([]int, size); + for i, _ := range count { + count[i] = 1 + } + + maxCount := 0 + for i := 1; i < size; i++ { + for j := 0; j < i; j++ { + if nums[i] > nums[j] { + if dp[j] + 1 > dp[i] { + dp[i] = dp[j] + 1 + count[i] = count[j] + } else if dp[j] + 1 == dp[i] { + count[i] += count[j] + } + } + if dp[i] > maxCount { + maxCount = dp[i] + } + } + } + + result := 0 + for i := 0; i < size; i++ { + if maxCount == dp[i] { + result += count[i] + } + } + return result +} ``` ## JavaScript From fa309c928d9b36e27c1bc57be973af3cc0b92489 Mon Sep 17 00:00:00 2001 From: hailincai Date: Fri, 27 Aug 2021 07:43:28 -0400 Subject: [PATCH 2/5] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md?= 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 f4abc535..455a3c33 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -80,7 +80,7 @@ dp状态图如下: * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -就知道了,01背包中二维dp数组的两个for遍历的先后循序是可以颠倒了,一位dp数组的两个for循环先后循序一定是先遍历物品,再遍历背包容量。 +就知道了,01背包中二维dp数组的两个for遍历的先后循序是可以颠倒了,一维dp数组的两个for循环先后循序一定是先遍历物品,再遍历背包容量。 **在完全背包中,对于一维dp数组来说,其实两个for循环嵌套顺序同样无所谓!** From 65edb41040dd0f7d4e5062ceee8d617886a62fa0 Mon Sep 17 00:00:00 2001 From: Wen Date: Fri, 27 Aug 2021 22:46:24 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=200059.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II.md=20Python3=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0059.螺旋矩阵II.md | 63 ++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 7d02ff15..60786542 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -191,33 +191,48 @@ class Solution { python: -```python +```python3 class Solution: + def generateMatrix(self, n: int) -> List[List[int]]: - left, right, up, down = 0, n-1, 0, n-1 - matrix = [ [0]*n for _ in range(n)] - num = 1 - while left<=right and up<=down: - # 填充左到右 - for i in range(left, right+1): - matrix[up][i] = num - num += 1 - up += 1 - # 填充上到下 - for i in range(up, down+1): - matrix[i][right] = num - num += 1 - right -= 1 - # 填充右到左 - for i in range(right, left-1, -1): - matrix[down][i] = num - num += 1 - down -= 1 - # 填充下到上 - for i in range(down, up-1, -1): - matrix[i][left] = num - num += 1 + # 初始化要填充的正方形 + matrix = [[0] * n for _ in range(n)] + + left, right, up, down = 0, n - 1, 0, n - 1 + number = 1 # 要填充的数字 + + while left < right and up < down: + + # 从左到右填充上边 + for x in range(left, right): + matrix[up][x] = number + number += 1 + + # 从上到下填充右边 + for y in range(up, down): + matrix[y][right] = number + number += 1 + + # 从右到左填充下边 + for x in range(right, left, -1): + matrix[down][x] = number + number += 1 + + # 从下到上填充左边 + for y in range(down, up, -1): + matrix[y][left] = number + number += 1 + + # 缩小要填充的范围 left += 1 + right -= 1 + up += 1 + down -= 1 + + # 如果阶数为奇数,额外填充一次中心 + if n % 2: + matrix[n // 2][n // 2] = number + return matrix ``` From a7d3ab675a0835b23c956cdf9acaf627fc7c4148 Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 29 Aug 2021 07:16:16 -0400 Subject: [PATCH 4/5] =?UTF-8?q?Update=200337.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix了一些md中的typo --- problems/0337.打家劫舍III.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 2a69ac56..dfb8ba57 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -29,7 +29,7 @@ 与198.打家劫舍,213.打家劫舍II一样,关键是要讨论当前节点抢还是不抢。 -如果抢了当前节点,两个孩子就不是动,如果没抢当前节点,就可以考虑抢左右孩子(**注意这里说的是“考虑”**) +如果抢了当前节点,两个孩子就不能动,如果没抢当前节点,就可以考虑抢左右孩子(**注意这里说的是“考虑”**) ### 暴力递归 @@ -91,7 +91,7 @@ public: ### 动态规划 -在上面两种方法,其实对一个节点 投与不投得到的最大金钱都没有做记录,而是需要实时计算。 +在上面两种方法,其实对一个节点 偷与不偷得到的最大金钱都没有做记录,而是需要实时计算。 而动态规划其实就是使用状态转移容器来记录状态的变化,这里可以使用一个长度为2的数组,记录当前节点偷与不偷所得到的的最大金钱。 @@ -121,7 +121,7 @@ vector robTree(TreeNode* cur) { 2. 确定终止条件 -在遍历的过程中,如果遇到空间点的话,很明显,无论偷还是不偷都是0,所以就返回 +在遍历的过程中,如果遇到空节点的话,很明显,无论偷还是不偷都是0,所以就返回 ``` if (cur == NULL) return vector{0, 0}; ``` From 15ca61a880747f6afbc2c20e8c1520652cf5ea15 Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 29 Aug 2021 07:37:38 -0400 Subject: [PATCH 5/5] =?UTF-8?q?Update=201005.K=E6=AC=A1=E5=8F=96=E5=8F=8D?= =?UTF-8?q?=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 保持Java代码的一致性,使用stream api来返回累加结果。 --- problems/1005.K次取反后最大化的数组和.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 8bdd0f41..30718b13 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -117,11 +117,8 @@ class Solution { } // 如果K还大于0,那么反复转变数值最小的元素,将K用完 if (k % 2 == 1) nums[len - 1] = -nums[len - 1]; - int result = 0; - for (int a : nums) { - result += a; - } - return result; + + return Arrays.stream(nums).sum(); } } ```