From 1739379b3d31dfe41b519ee4f2eca3da03e53b79 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 19 Jun 2022 21:57:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200416.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md=20Scala?= =?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/0416.分割等和子集.md | 55 +++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index eb6601e1..e14287e6 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -183,7 +183,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java class Solution { public boolean canPartition(int[] nums) { @@ -316,7 +316,7 @@ class Solution { } } ``` -Python: +### Python: ```python class Solution: def canPartition(self, nums: List[int]) -> bool: @@ -329,7 +329,7 @@ class Solution: dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) return target == dp[target] ``` -Go: +### Go: ```go // 分割等和子集 动态规划 // 时间复杂度O(n^2) 空间复杂度O(n) @@ -397,7 +397,7 @@ func canPartition(nums []int) bool { } ``` -javaScript: +### javaScript: ```js var canPartition = function(nums) { @@ -417,7 +417,7 @@ var canPartition = function(nums) { ``` -C: +### C: 二维dp: ```c /** @@ -518,7 +518,7 @@ bool canPartition(int* nums, int numsSize){ } ``` -TypeScript: +### TypeScript: > 一维数组,简洁 @@ -573,7 +573,50 @@ function canPartition(nums: number[]): boolean { }; ``` +### Scala +滚动数组: +```scala +object Solution { + def canPartition(nums: Array[Int]): Boolean = { + var sum = nums.sum + if (sum % 2 != 0) return false + var half = sum / 2 + var dp = new Array[Int](half + 1) + + // 遍历 + for (i <- 0 until nums.length; j <- half to nums(i) by -1) { + dp(j) = math.max(dp(j), dp(j - nums(i)) + nums(i)) + } + + if (dp(half) == half) true else false + } +} +``` + +二维数组: +```scala +object Solution { + def canPartition(nums: Array[Int]): Boolean = { + var sum = nums.sum + if (sum % 2 != 0) return false + var half = sum / 2 + var dp = Array.ofDim[Int](nums.length, half + 1) + + // 初始化 + for (j <- nums(0) to half) dp(0)(j) = nums(0) + + // 遍历 + for (i <- 1 until nums.length; j <- 1 to half) { + if (j - nums(i) >= 0) dp(i)(j) = nums(i) + dp(i - 1)(j - nums(i)) + dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j)) + } + + // 如果等于half就返回ture,否则返回false + if (dp(nums.length - 1)(half) == half) true else false + } +} +``` ----------------------- From 910dc88d54a098b374b7c137873e3711710d68f9 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 19 Jun 2022 22:50:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201049.=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E9=87=8FII.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1049.最后一块石头的重量II.md | 50 +++++++++++++++++-- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index 3d256c3d..1e3b958f 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -152,7 +152,7 @@ public: ## 其他语言版本 -Java: +### Java: 一维数组版本 ```Java @@ -212,7 +212,7 @@ class Solution { ``` -Python: +### Python: ```python class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: @@ -225,7 +225,7 @@ class Solution: return sumweight - 2 * dp[target] ``` -Go: +### Go: ```go func lastStoneWeightII(stones []int) int { // 15001 = 30 * 1000 /2 +1 @@ -254,7 +254,7 @@ func max(a, b int) int { } ``` -JavaScript版本 +### JavaScript ```javascript /** @@ -277,7 +277,7 @@ var lastStoneWeightII = function (stones) { }; ``` -TypeScript: +### TypeScript: ```typescript function lastStoneWeightII(stones: number[]): number { @@ -296,7 +296,47 @@ function lastStoneWeightII(stones: number[]): number { }; ``` +### Scala +滚动数组: +```scala +object Solution { + def lastStoneWeightII(stones: Array[Int]): Int = { + var sum = stones.sum + var half = sum / 2 + var dp = new Array[Int](half + 1) + + // 遍历 + for (i <- 0 until stones.length; j <- half to stones(i) by -1) { + dp(j) = math.max(dp(j), dp(j - stones(i)) + stones(i)) + } + + sum - 2 * dp(half) + } +} +``` + +二维数组: +```scala +object Solution { + def lastStoneWeightII(stones: Array[Int]): Int = { + var sum = stones.sum + var half = sum / 2 + var dp = Array.ofDim[Int](stones.length, half + 1) + + // 初始化 + for (j <- stones(0) to half) dp(0)(j) = stones(0) + + // 遍历 + for (i <- 1 until stones.length; j <- 1 to half) { + if (j - stones(i) >= 0) dp(i)(j) = stones(i) + dp(i - 1)(j - stones(i)) + dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j)) + } + + sum - 2 * dp(stones.length - 1)(half) + } +} +``` -----------------------