From 0a3a2d4ae2213cad03d8f339f09f4e9de7420998 Mon Sep 17 00:00:00 2001 From: Rinko Taketsuki <33001553+RinkoTaketsuki@users.noreply.github.com> Date: Mon, 13 Jun 2022 09:27:00 +0800 Subject: [PATCH 01/61] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index a51c68ee..1d8a0e64 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -49,7 +49,7 @@ 数组长度为:len。 -如果len % (len - (next[len - 1] + 1)) == 0 ,则说明 (数组长度-最长相等前后缀的长度) 正好可以被 数组的长度整除,说明有该字符串有重复的子字符串。 +如果len % (len - (next[len - 1] + 1)) == 0 ,则说明数组的长度正好可以被 (数组长度-最长相等前后缀的长度) 整除 ,说明该字符串有重复的子字符串。 **数组长度减去最长相同前后缀的长度相当于是第一个周期的长度,也就是一个周期的长度,如果这个周期可以被整除,就说明整个数组就是这个周期的循环。** From 32018ff79afa8fb65cdf69445c9d93a34e648d54 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 13 Jun 2022 20:53:11 +0800 Subject: [PATCH 02/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200135.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0135.分发糖果.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index 3456a04c..a805d0d4 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -324,6 +324,31 @@ function candy(ratings: number[]): number { }; ``` +### Scala + +```scala +object Solution { + def candy(ratings: Array[Int]): Int = { + var candyVec = new Array[Int](ratings.length) + for (i <- candyVec.indices) candyVec(i) = 1 + // 从前向后 + for (i <- 1 until candyVec.length) { + if (ratings(i) > ratings(i - 1)) { + candyVec(i) = candyVec(i - 1) + 1 + } + } + + // 从后向前 + for (i <- (candyVec.length - 2) to 0 by -1) { + if (ratings(i) > ratings(i + 1)) { + candyVec(i) = math.max(candyVec(i), candyVec(i + 1) + 1) + } + } + + candyVec.sum // 求和 + } +} +``` ----------------------- From 6867c9c5bfd379a2f6e8afb09026cb0e759bc8dc Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 13 Jun 2022 21:21:22 +0800 Subject: [PATCH 03/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200860.=E6=9F=A0?= =?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0860.柠檬水找零.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index aa09e1c6..4a676b43 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -328,6 +328,37 @@ function lemonadeChange(bills: number[]): boolean { ``` +### Scala + +```scala +object Solution { + def lemonadeChange(bills: Array[Int]): Boolean = { + var fiveNum = 0 + var tenNum = 0 + + for (i <- bills) { + if (i == 5) fiveNum += 1 + if (i == 10) { + if (fiveNum <= 0) return false + tenNum += 1 + fiveNum -= 1 + } + if (i == 20) { + if (fiveNum > 0 && tenNum > 0) { + tenNum -= 1 + fiveNum -= 1 + } else if (fiveNum >= 3) { + fiveNum -= 3 + } else { + return false + } + } + } + true + } +} +``` + -----------------------
From 17208d89290c4bc10256414393671022b6b96ccf Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 13 Jun 2022 22:12:49 +0800 Subject: [PATCH 04/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0406.根据身高重建队列.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index 641086a9..516df7d7 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -354,8 +354,27 @@ function reconstructQueue(people: number[][]): number[][] { }; ``` +### Scala +```scala +object Solution { + import scala.collection.mutable + def reconstructQueue(people: Array[Array[Int]]): Array[Array[Int]] = { + val person = people.sortWith((a, b) => { + if (a(0) == b(0)) a(1) < b(1) + else a(0) > b(0) + }) + var que = mutable.ArrayBuffer[Array[Int]]() + + for (per <- person) { + que.insert(per(1), per) + } + + que.toArray + } +} +``` ----------------------- From e391bf18f7f0c607a918413d26866a9f376ced1e Mon Sep 17 00:00:00 2001 From: van_fantasy <46948123+sexyxlyWAol@users.noreply.github.com> Date: Mon, 13 Jun 2022 22:44:49 +0800 Subject: [PATCH 05/61] =?UTF-8?q?Update=201221.=E5=88=86=E5=89=B2=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added python and go solution to 1221 --- problems/1221.分割平衡字符串.md | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/1221.分割平衡字符串.md b/problems/1221.分割平衡字符串.md index 1a9b34a2..e18c6358 100644 --- a/problems/1221.分割平衡字符串.md +++ b/problems/1221.分割平衡字符串.md @@ -108,11 +108,38 @@ class Solution { ### Python ```python +class Solution: + def balancedStringSplit(self, s: str) -> int: + diff = 0 #右左差值 + ans = 0 + for c in s: + if c == "L": + diff -= 1 + else: + diff += 1 + if tilt == 0: + ans += 1 + return ans ``` ### Go ```go +func balancedStringSplit(s string) int { + diff := 0 // 右左差值 + ans := 0 + for _, c := range s { + if c == 'L' { + diff-- + }else { + diff++ + } + if diff == 0 { + ans++ + } + } + return ans +} ``` ### JavaScript From 8f52696f2206f2361917aad3e1cc7f87698c5cc0 Mon Sep 17 00:00:00 2001 From: Rinko Taketsuki <33001553+RinkoTaketsuki@users.noreply.github.com> Date: Tue, 14 Jun 2022 09:24:58 +0800 Subject: [PATCH 06/61] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 1d8a0e64..ba2fa160 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -63,7 +63,7 @@ next[len - 1] = 7,next[len - 1] + 1 = 8,8就是此时字符串asdfasdfasdf的最长相同前后缀的长度。 -(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 4正好可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。 +(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 12 正好可以被 4 整除,所以说明有重复的子字符串(asdf)。 C++代码如下:(这里使用了前缀表统一减一的实现方式) From e8a8db47cfc42380b0979e8efcd7f9908216b2a6 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 14 Jun 2022 20:03:34 +0800 Subject: [PATCH 07/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200452.=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?= =?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0452.用最少数量的箭引爆气球.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index d4bbe961..e07aa6e6 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -288,5 +288,30 @@ impl Solution { } } ``` + +### Scala + +```scala +object Solution { + def findMinArrowShots(points: Array[Array[Int]]): Int = { + if (points.length == 0) return 0 + // 排序 + var point = points.sortWith((a, b) => { + a(0) < b(0) + }) + + var result = 1 // points不为空就至少需要一只箭 + for (i <- 1 until point.length) { + if (point(i)(0) > point(i - 1)(1)) { + result += 1 + } else { + point(i)(1) = math.min(point(i - 1)(1), point(i)(1)) + } + } + result // 返回结果 + } +} +``` + -----------------------
From ece2c3efb6e492529c4c8b9b4be1bdf7fbb1b8b6 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 14 Jun 2022 20:45:13 +0800 Subject: [PATCH 08/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200435.=E6=97=A0?= =?UTF-8?q?=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0435.无重叠区间.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index 66aa1244..6313bc44 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -352,7 +352,27 @@ function eraseOverlapIntervals(intervals: number[][]): number { }; ``` +### Scala +```scala +object Solution { + def eraseOverlapIntervals(intervals: Array[Array[Int]]): Int = { + var result = 0 + var interval = intervals.sortWith((a, b) => { + a(1) < b(1) + }) + var edge = Int.MinValue + for (i <- 0 until interval.length) { + if (edge <= interval(i)(0)) { + edge = interval(i)(1) + } else { + result += 1 + } + } + result + } +} +``` From 94350c0c99107f5343a589d36382e52a8a3c0059 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 14 Jun 2022 22:26:56 +0800 Subject: [PATCH 09/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200763.=E5=88=92?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.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/0763.划分字母区间.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 2f4d1b48..f7b16f5c 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -317,7 +317,31 @@ function partitionLabels(s: string): number[] { }; ``` +### Scala +```scala +object Solution { + import scala.collection.mutable + def partitionLabels(s: String): List[Int] = { + var hash = new Array[Int](26) + for (i <- s.indices) { + hash(s(i) - 'a') = i + } + + var res = mutable.ListBuffer[Int]() + var (left, right) = (0, 0) + for (i <- s.indices) { + right = math.max(hash(s(i) - 'a'), right) + if (i == right) { + res.append(right - left + 1) + left = i + 1 + } + } + + res.toList + } +} +``` ----------------------- From eddfde7c1cb4c7db4c11f7d6c76b8a732a65d313 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Wed, 15 Jun 2022 22:25:35 +0800 Subject: [PATCH 10/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200056.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8C=BA=E9=97=B4.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0056.合并区间.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index e444a221..38e3472c 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -286,7 +286,37 @@ function merge(intervals: number[][]): number[][] { }; ``` +### Scala +```scala +object Solution { + import scala.collection.mutable + def merge(intervals: Array[Array[Int]]): Array[Array[Int]] = { + var res = mutable.ArrayBuffer[Array[Int]]() + + // 排序 + var interval = intervals.sortWith((a, b) => { + a(0) < b(0) + }) + + var left = interval(0)(0) + var right = interval(0)(1) + + for (i <- 1 until interval.length) { + if (interval(i)(0) <= right) { + left = math.min(left, interval(i)(0)) + right = math.max(right, interval(i)(1)) + } else { + res.append(Array[Int](left, right)) + left = interval(i)(0) + right = interval(i)(1) + } + } + res.append(Array[Int](left, right)) + res.toArray // 返回res的Array形式 + } +} +``` -----------------------
From 23c26135135f39757932a158d5c4b9f00f4bfe3b Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Wed, 15 Jun 2022 23:15:29 +0800 Subject: [PATCH 11/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200738.=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md=20Sca?= =?UTF-8?q?la=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0738.单调递增的数字.md | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 4e4079a7..2911e1cc 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -246,7 +246,37 @@ function monotoneIncreasingDigits(n: number): number { ``` +### Scala +直接转换为了整数数组: +```scala +object Solution { + import scala.collection.mutable + def monotoneIncreasingDigits(n: Int): Int = { + var digits = mutable.ArrayBuffer[Int]() + // 提取每位数字 + var temp = n // 因为 参数n 是不可变量所以需要赋值给一个可变量 + while (temp != 0) { + digits.append(temp % 10) + temp = temp / 10 + } + // 贪心 + var flag = -1 + for (i <- 0 until (digits.length - 1) if digits(i) < digits(i + 1)) { + flag = i + digits(i + 1) -= 1 + } + for (i <- 0 to flag) digits(i) = 9 + + // 拼接 + var res = 0 + for (i <- 0 until digits.length) { + res += digits(i) * math.pow(10, i).toInt + } + res + } +} +``` -----------------------
From cff9cf34b0725b43598c82faccda24d83e0a06ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=9D=E9=86=89=E7=9A=84=E7=8E=A9=E5=85=B7=E7=86=8A=5F?= =?UTF-8?q?=E7=8E=8B=E5=9D=87=E7=A5=A5?= <1033076925@qq.com> Date: Thu, 16 Jun 2022 13:16:02 +0800 Subject: [PATCH 12/61] =?UTF-8?q?=E4=BF=AE=E6=AD=A30035.=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0035.搜索插入位置.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 5cf44ded..aef091af 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -142,7 +142,7 @@ public: ``` * 时间复杂度:O(log n) -* 时间复杂度:O(1) +* 空间复杂度:O(1) 效率如下: ![35_搜索插入位置2](https://img-blog.csdnimg.cn/2020121623272877.png) From a031937e874267a3a4dcbaf42eba272b244fc0fc Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 16 Jun 2022 22:32:13 +0800 Subject: [PATCH 13/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200714.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9.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 --- ...买卖股票的最佳时机含手续费.md | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index b27631c6..4bc21a70 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -153,7 +153,7 @@ public: ## 其他语言版本 -Java: +### Java ```java // 贪心思路 class Solution { @@ -198,7 +198,7 @@ class Solution { // 动态规划 -Python: +### Python ```python class Solution: # 贪心思路 @@ -216,7 +216,7 @@ class Solution: # 贪心思路 return result ``` -Go: +### Go ```golang func maxProfit(prices []int, fee int) int { var minBuy int = prices[0] //第一天买入 @@ -241,7 +241,7 @@ func maxProfit(prices []int, fee int) int { return res } ``` -Javascript: +### Javascript ```Javascript // 贪心思路 var maxProfit = function(prices, fee) { @@ -293,7 +293,7 @@ var maxProfit = function(prices, fee) { }; ``` -TypeScript: +### TypeScript > 贪心 @@ -335,8 +335,28 @@ function maxProfit(prices: number[], fee: number): number { }; ``` +### Scala +贪心思路: +```scala +object Solution { + def maxProfit(prices: Array[Int], fee: Int): Int = { + var result = 0 + var minPrice = prices(0) + for (i <- 1 until prices.length) { + if (prices(i) < minPrice) { + minPrice = prices(i) // 比当前最小值还小 + } + if (prices(i) > minPrice + fee) { + result += prices(i) - minPrice - fee + minPrice = prices(i) - fee + } + } + result + } +} +``` -----------------------
From da559f0ef013bb0503919bda084b244975a73ed6 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 19:17:49 +0800 Subject: [PATCH 14/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200968.=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0968.监控二叉树.md | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index 9a510a1b..6c957eb2 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -544,5 +544,40 @@ int minCameraCover(struct TreeNode* root){ } ``` +### Scala + +```scala +object Solution { + def minCameraCover(root: TreeNode): Int = { + var result = 0 + def traversal(cur: TreeNode): Int = { + // 空节点,该节点有覆盖 + if (cur == null) return 2 + var left = traversal(cur.left) + var right = traversal(cur.right) + // 情况1,左右节点都有覆盖 + if (left == 2 && right == 2) { + return 0 + } + // 情况2 + if (left == 0 || right == 0) { + result += 1 + return 1 + } + // 情况3 + if (left == 1 || right == 1) { + return 2 + } + -1 + } + + if (traversal(root) == 0) { + result += 1 + } + result + } +} +``` + -----------------------
From cf0affde0826eedf6328cf383d89d70028bf43a6 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 19:30:51 +0800 Subject: [PATCH 15/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200509.=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 1d17784d..d8e4e1d7 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -245,7 +245,7 @@ var fib = function(n) { }; ``` -TypeScript +### TypeScript ```typescript function fib(n: number): number { @@ -324,5 +324,33 @@ pub fn fib(n: i32) -> i32 { return fib(n - 1) + fib(n - 2); } ``` + +### Scala + +动态规划: +```scala +object Solution { + def fib(n: Int): Int = { + if (n <= 1) return n + var dp = new Array[Int](n + 1) + dp(1) = 1 + for (i <- 2 to n) { + dp(i) = dp(i - 1) + dp(i - 2) + } + dp(n) + } +} +``` + +递归: +```scala +object Solution { + def fib(n: Int): Int = { + if (n <= 1) return n + fib(n - 1) + fib(n - 2) + } +} +``` + -----------------------
From 5985aae83ce72f2d25b4976a836c1a5fd39ac1ef Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 19:37:35 +0800 Subject: [PATCH 16/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 34d41441..097466b0 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -401,6 +401,38 @@ int climbStairs(int n){ } ``` +### Scala + +```scala +object Solution { + def climbStairs(n: Int): Int = { + if (n <= 2) return n + var dp = new Array[Int](n + 1) + dp(1) = 1 + dp(2) = 2 + for (i <- 3 to n) { + dp(i) = dp(i - 1) + dp(i - 2) + } + dp(n) + } +} +``` + +优化空间复杂度: +```scala +object Solution { + def climbStairs(n: Int): Int = { + if (n <= 2) return n + var (a, b) = (1, 2) + for (i <- 3 to n) { + var tmp = a + b + a = b + b = tmp + } + b // 最终返回b + } +} +``` -----------------------
From 6e92cd2417dea438d7671140fd73298a4b598c93 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 20:12:19 +0800 Subject: [PATCH 17/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 5931fc8a..abaeb980 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -305,5 +305,35 @@ int minCostClimbingStairs(int* cost, int costSize){ return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]; } ``` + +### Scala + +```scala +object Solution { + def minCostClimbingStairs(cost: Array[Int]): Int = { + var dp = new Array[Int](cost.length) + dp(0) = cost(0) + dp(1) = cost(1) + for (i <- 2 until cost.length) { + dp(i) = math.min(dp(i - 1), dp(i - 2)) + cost(i) + } + math.min(dp(cost.length - 1), dp(cost.length - 2)) + } +} +``` + +第二种思路: dp[i] 表示爬到第i-1层所需的最小花费,状态转移方程为: dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]) +```scala +object Solution { + def minCostClimbingStairs(cost: Array[Int]): Int = { + var dp = new Array[Int](cost.length + 1) + for (i <- 2 until cost.length + 1) { + dp(i) = math.min(dp(i - 1) + cost(i - 1), dp(i - 2) + cost(i - 2)) + } + dp(cost.length) + } +} +``` + -----------------------
From 8e27191aec0f9c377fe79b61a1b6deffa2af5fe4 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 20:29:53 +0800 Subject: [PATCH 18/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200062.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0062.不同路径.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index f59b7be8..cccda7f1 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -412,5 +412,21 @@ int uniquePaths(int m, int n){ } ``` +### Scala + +```scala +object Solution { + def uniquePaths(m: Int, n: Int): Int = { + var dp = Array.ofDim[Int](m, n) + for (i <- 0 until m) dp(i)(0) = 1 + for (j <- 1 until n) dp(0)(j) = 1 + for (i <- 1 until m; j <- 1 until n) { + dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1) + } + dp(m - 1)(n - 1) + } +} +``` + -----------------------
From a9de01b83c02ab57181b52df37f4252b5709772f Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 20:49:46 +0800 Subject: [PATCH 19/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 59c60156..88fce505 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -440,5 +440,37 @@ int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obst } ``` +### Scala + +```scala +object Solution { + import scala.util.control.Breaks._ + def uniquePathsWithObstacles(obstacleGrid: Array[Array[Int]]): Int = { + var (m, n) = (obstacleGrid.length, obstacleGrid(0).length) + var dp = Array.ofDim[Int](m, n) + + // 比如break、continue这些流程控制需要使用breakable + breakable( + for (i <- 0 until m) { + if (obstacleGrid(i)(0) != 1) dp(i)(0) = 1 + else break() + } + ) + breakable( + for (j <- 0 until n) { + if (obstacleGrid(0)(j) != 1) dp(0)(j) = 1 + else break() + } + ) + + for (i <- 1 until m; j <- 1 until n; if obstacleGrid(i)(j) != 1) { + dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1) + } + + dp(m - 1)(n - 1) + } +} +``` + -----------------------
From 568e36bae8ba2f2812a47a2d7548b9ebd20041b5 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 21:45:03 +0800 Subject: [PATCH 20/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200343.=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E6=8B=86=E5=88=86.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0343.整数拆分.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 279f1d71..9166f2cb 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -335,5 +335,22 @@ int integerBreak(int n){ } ``` +### Scala + +```scala +object Solution { + def integerBreak(n: Int): Int = { + var dp = new Array[Int](n + 1) + dp(2) = 1 + for (i <- 3 to n) { + for (j <- 1 until i - 1) { + dp(i) = math.max(dp(i), math.max(j * (i - j), j * dp(i - j))) + } + } + dp(n) + } +} +``` + -----------------------
From 374190e2232ec9daeffe20660d1020199a17c10b Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 22:25:08 +0800 Subject: [PATCH 21/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200096.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= =?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0096.不同的二叉搜索树.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index 25561b50..a33421ae 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -227,7 +227,7 @@ const numTrees =(n) => { }; ``` -TypeScript +### TypeScript ```typescript function numTrees(n: number): number { @@ -282,5 +282,22 @@ int numTrees(int n){ } ``` +### Scala + +```scala +object Solution { + def numTrees(n: Int): Int = { + var dp = new Array[Int](n + 1) + dp(0) = 1 + for (i <- 1 to n) { + for (j <- 1 to i) { + dp(i) += dp(j - 1) * dp(i - j) + } + } + dp(n) + } +} +``` + -----------------------
From 106430d0083351c958e90745103747ea8fe7399e Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 19 Jun 2022 21:20:49 +0800 Subject: [PATCH 22/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= =?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-1.md | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index a40a92ab..f9916667 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -498,7 +498,41 @@ const size = 4; console.log(testWeightBagProblem(weight, value, size)); ``` +### Scala +```scala +object Solution { + // 01背包 + def test_2_wei_bag_problem1(): Unit = { + var weight = Array[Int](1, 3, 4) + var value = Array[Int](15, 20, 30) + var baseweight = 4 + + // 二维数组 + var dp = Array.ofDim[Int](weight.length, baseweight + 1) + + // 初始化 + for (j <- weight(0) to baseweight) { + dp(0)(j) = value(0) + } + + // 遍历 + for (i <- 1 until weight.length; j <- 1 to baseweight) { + if (j - weight(i) >= 0) dp(i)(j) = dp(i - 1)(j - weight(i)) + value(i) + dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j)) + } + + // 打印数组 + dp.foreach(x => println("[" + x.mkString(",") + "]")) + + dp(weight.length - 1)(baseweight) // 最终返回 + } + + def main(args: Array[String]): Unit = { + test_2_wei_bag_problem1() + } +} +``` -----------------------
From a2d340b00279aac08df7cbba7a5733416dc28920 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 19 Jun 2022 21:32:10 +0800 Subject: [PATCH 23/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= =?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-2.md | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index b66b74a6..81e61be4 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -375,7 +375,33 @@ console.log(testWeightBagProblem(weight, value, size)); ``` +### Scala +```scala +object Solution { + // 滚动数组 + def test_1_wei_bag_problem(): Unit = { + var weight = Array[Int](1, 3, 4) + var value = Array[Int](15, 20, 30) + var baseweight = 4 + + // dp数组 + var dp = new Array[Int](baseweight + 1) + + // 遍历 + for (i <- 0 until weight.length; j <- baseweight to weight(i) by -1) { + dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i)) + } + + // 打印数组 + println("[" + dp.mkString(",") + "]") + } + + def main(args: Array[String]): Unit = { + test_1_wei_bag_problem() + } +} +``` -----------------------
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 24/61] =?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 25/61] =?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) + } +} +``` -----------------------
From 4cb3897549197115721472552bbfb46e3848d682 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 20 Jun 2022 21:19:31 +0800 Subject: [PATCH 26/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200494.=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 8ce1f6f1..639f8ee8 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -250,7 +250,7 @@ dp[j] += dp[j - nums[i]]; ## 其他语言版本 -Java: +### Java ```java class Solution { public int findTargetSumWays(int[] nums, int target) { @@ -271,7 +271,7 @@ class Solution { } ``` -Python: +### Python ```python class Solution: def findTargetSumWays(self, nums: List[int], target: int) -> int: @@ -287,7 +287,7 @@ class Solution: return dp[bagSize] ``` -Go: +### Go ```go func findTargetSumWays(nums []int, target int) int { sum := 0 @@ -322,7 +322,7 @@ func abs(x int) int { } ``` -Javascript: +### Javascript ```javascript const findTargetSumWays = (nums, target) => { @@ -351,7 +351,7 @@ const findTargetSumWays = (nums, target) => { }; ``` -TypeScript: +### TypeScript ```typescript function findTargetSumWays(nums: number[], target: number): number { @@ -370,7 +370,25 @@ function findTargetSumWays(nums: number[], target: number): number { }; ``` +### Scala +```scala +object Solution { + def findTargetSumWays(nums: Array[Int], target: Int): Int = { + var sum = nums.sum + if (math.abs(target) > sum) return 0 // 此时没有方案 + if ((sum + target) % 2 == 1) return 0 // 此时没有方案 + var bagSize = (sum + target) / 2 + var dp = new Array[Int](bagSize + 1) + dp(0) = 1 + for (i <- 0 until nums.length; j <- bagSize to nums(i) by -1) { + dp(j) += dp(j - nums(i)) + } + + dp(bagSize) + } +} +``` -----------------------
From c99bf39601a06278f9cca2e5f6d61638e23f3ced Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 23 Jun 2022 21:34:24 +0800 Subject: [PATCH 27/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200474.=E4=B8=80?= =?UTF-8?q?=E5=92=8C=E9=9B=B6.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 83 +++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index d38ce03f..d6c598aa 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -163,7 +163,7 @@ public: ## 其他语言版本 -Java: +### Java ```Java class Solution { public int findMaxForm(String[] strs, int m, int n) { @@ -192,7 +192,7 @@ class Solution { } ``` -Python: +### Python ```python class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: @@ -208,7 +208,7 @@ class Solution: return dp[m][n] ``` -Go: +### Go ```go func findMaxForm(strs []string, m int, n int) int { // 定义数组 @@ -294,7 +294,7 @@ func getMax(a,b int)int{ } ``` -Javascript: +### Javascript ```javascript const findMaxForm = (strs, m, n) => { const dp = Array.from(Array(m+1), () => Array(n+1).fill(0)); @@ -323,7 +323,7 @@ const findMaxForm = (strs, m, n) => { }; ``` -TypeScript: +### TypeScript > 滚动数组,二维数组法 @@ -446,7 +446,80 @@ function isValidSubSet(strs: string[], m: number, n: number): boolean { } ``` +### Scala +背包: +```scala +object Solution { + def findMaxForm(strs: Array[String], m: Int, n: Int): Int = { + var dp = Array.ofDim[Int](m + 1, n + 1) + + var (oneNum, zeroNum) = (0, 0) + + for (str <- strs) { + oneNum = 0 + zeroNum = 0 + for (i <- str.indices) { + if (str(i) == '0') zeroNum += 1 + else oneNum += 1 + } + + for (i <- m to zeroNum by -1) { + for (j <- n to oneNum by -1) { + dp(i)(j) = math.max(dp(i)(j), dp(i - zeroNum)(j - oneNum) + 1) + } + } + } + + dp(m)(n) + } +} +``` + +回溯法(超时): +```scala +object Solution { + import scala.collection.mutable + + var res = Int.MinValue + + def test(str: String): (Int, Int) = { + var (zero, one) = (0, 0) + for (i <- str.indices) { + if (str(i) == '1') one += 1 + else zero += 1 + } + (zero, one) + } + + def travsel(strs: Array[String], path: mutable.ArrayBuffer[String], m: Int, n: Int, startIndex: Int): Unit = { + if (startIndex > strs.length) { + return + } + + res = math.max(res, path.length) + + for (i <- startIndex until strs.length) { + + var (zero, one) = test(strs(i)) + + // 如果0的个数小于m,1的个数小于n,则可以回溯 + if (zero <= m && one <= n) { + path.append(strs(i)) + travsel(strs, path, m - zero, n - one, i + 1) + path.remove(path.length - 1) + } + } + } + + def findMaxForm(strs: Array[String], m: Int, n: Int): Int = { + res = Int.MinValue + var path = mutable.ArrayBuffer[String]() + travsel(strs, path, m, n, 0) + res + } +} +``` -----------------------
From e9edda44e0cf080701614a1df8ab680a264c4e40 Mon Sep 17 00:00:00 2001 From: shutengfei Date: Sat, 25 Jun 2022 15:56:26 +0800 Subject: [PATCH 28/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880649.Dota2?= =?UTF-8?q?=E5=8F=82=E8=AE=AE=E9=99=A2.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0649.Dota2参议院.md | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0649.Dota2参议院.md b/problems/0649.Dota2参议院.md index 6e84c9fd..264a003a 100644 --- a/problems/0649.Dota2参议院.md +++ b/problems/0649.Dota2参议院.md @@ -244,6 +244,44 @@ var predictPartyVictory = function(senateStr) { }; ``` +## TypeScript + +```typescript +function predictPartyVictory(senate: string): string { + // 数量差:Count(Radiant) - Count(Dire) + let deltaRDCnt: number = 0; + let hasR: boolean = true, + hasD: boolean = true; + const senateArr: string[] = senate.split(''); + while (hasR && hasD) { + hasR = false; + hasD = false; + for (let i = 0, length = senateArr.length; i < length; i++) { + if (senateArr[i] === 'R') { + if (deltaRDCnt < 0) { + senateArr[i] = ''; + } else { + hasR = true; + } + deltaRDCnt++; + } else if (senateArr[i] === 'D') { + if (deltaRDCnt > 0) { + senateArr[i] = ''; + } else { + hasD = true; + } + deltaRDCnt--; + } + } + } + return hasR ? 'Radiant' : 'Dire'; +}; +``` + + + + + -----------------------
From dd31d67778cea30688bd60e4e0b23631758538e8 Mon Sep 17 00:00:00 2001 From: lesenelir Date: Sun, 26 Jun 2022 11:24:35 +0800 Subject: [PATCH 29/61] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A(1049.?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E9=87=8FII.md):=20=E4=BF=AE=E6=94=B9markdown=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1049.最后一块石头的重量II.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index f3e7909c..1e87848e 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -3,7 +3,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 动态规划:最后一块石头的重量 II ## 1049. 最后一块石头的重量 II From 88ca27562e748e004ac75dfd9ad89c2fc8fc7f07 Mon Sep 17 00:00:00 2001 From: Yang Date: Sun, 26 Jun 2022 10:08:22 +0200 Subject: [PATCH 30/61] Remove redundant `break` in the for loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove redundant `break` in the for loop of python script. When the `return` is triggered, the for loop will break automatically. PS: really like your work! Thanks a lot 😄 . --- problems/0242.有效的字母异位词.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 8fd9c604..f1f7e6cf 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -125,8 +125,6 @@ class Solution: if record[i] != 0: #record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。 return False - #如果有一个元素不为零,则可以判断字符串s和t不是字母异位词 - break return True ``` From 8bc026fed035ee9c0f2f8b0452f7e0a5f5531281 Mon Sep 17 00:00:00 2001 From: UndeadSheep Date: Mon, 27 Jun 2022 15:28:47 +0800 Subject: [PATCH 31/61] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=A0=88=E4=B8=8E?= =?UTF-8?q?=E9=98=9F=E5=88=97=E9=83=A8=E5=88=86=E7=9A=84=20C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 31 ++++++++++++++ problems/0150.逆波兰表达式求值.md | 34 +++++++++++++++ problems/0225.用队列实现栈.md | 35 ++++++++++++++++ problems/0232.用栈实现队列.md | 41 +++++++++++++++++++ problems/0239.滑动窗口最大值.md | 41 +++++++++++++++++++ problems/0347.前K个高频元素.md | 35 ++++++++++++++++ ...除字符串中的所有相邻重复项.md | 17 ++++++++ 7 files changed, 234 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 7bb7f746..cdca70f9 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -401,5 +401,36 @@ bool isValid(char * s){ } ``` +C#: +```csharp +public class Solution { + public bool IsValid(string s) { + var len = s.Length; + if(len % 2 == 1) return false; // 字符串长度为单数,直接返回 false + // 初始化栈 + var stack = new Stack(); + // 遍历字符串 + for(int i = 0; i < len; i++){ + // 当字符串为左括号时,进栈对应的右括号 + if(s[i] == '('){ + stack.Push(')'); + }else if(s[i] == '['){ + stack.Push(']'); + }else if(s[i] == '{'){ + stack.Push('}'); + } + // 当字符串为右括号时,当栈为空(无左括号) 或者 出栈字符不是当前的字符 + else if(stack.Count == 0 || stack.Pop() != s[i]) + return false; + } + // 如果栈不为空,例如“((()”,右括号少于左括号,返回false + if (stack.Count > 0) + return false; + // 上面的校验都满足,则返回true + else + return true; + } +} +``` -----------------------
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index f4dad823..8dafa460 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -326,5 +326,39 @@ func evalRPN(_ tokens: [String]) -> Int { } ``` +C#: +```csharp +public int EvalRPN(string[] tokens) { + int num; + Stack stack = new Stack(); + foreach(string s in tokens){ + if(int.TryParse(s, out num)){ + stack.Push(num); + }else{ + int num1 = stack.Pop(); + int num2 = stack.Pop(); + switch (s) + { + case "+": + stack.Push(num1 + num2); + break; + case "-": + stack.Push(num2 - num1); + break; + case "*": + stack.Push(num1 * num2); + break; + case "/": + stack.Push(num2 / num1); + break; + default: + break; + } + } + } + return stack.Pop(); + } +``` + -----------------------
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 3457c4b3..5d902fb1 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -816,5 +816,40 @@ class MyStack { } ``` +C#: +```csharp +public class MyStack { + Queue queue1; + Queue queue2; + public MyStack() { + queue1 = new Queue(); + queue2 = new Queue(); + } + + public void Push(int x) { + queue2.Enqueue(x); + while(queue1.Count != 0){ + queue2.Enqueue(queue1.Dequeue()); + } + Queue queueTemp; + queueTemp = queue1; + queue1 = queue2; + queue2 = queueTemp; + } + + public int Pop() { + return queue1.Count > 0 ? queue1.Dequeue() : -1; + } + + public int Top() { + return queue1.Count > 0 ? queue1.Peek() : -1; + } + + public bool Empty() { + return queue1.Count == 0; + } +} + +``` -----------------------
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1a56d9f3..d58dc55f 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -496,5 +496,46 @@ void myQueueFree(MyQueue* obj) { } ``` +C#: +```csharp +public class MyQueue { + Stack inStack; + Stack outStack; + + public MyQueue() { + inStack = new Stack();// 负责进栈 + outStack = new Stack();// 负责出栈 + } + + public void Push(int x) { + inStack.Push(x); + } + + public int Pop() { + dumpstackIn(); + return outStack.Pop(); + } + + public int Peek() { + dumpstackIn(); + return outStack.Peek(); + } + + public bool Empty() { + return inStack.Count == 0 && outStack.Count == 0; + } + + // 处理方法: + // 如果outStack为空,那么将inStack中的元素全部放到outStack中 + private void dumpstackIn(){ + if (outStack.Count != 0) return; + while(inStack.Count != 0){ + outStack.Push(inStack.Pop()); + } + } +} + +``` + -----------------------
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index f269450f..35ca1eed 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -631,5 +631,46 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { } ``` +C#: +```csharp +class myDequeue{ + private LinkedList linkedList = new LinkedList(); + + public void Enqueue(int n){ + while(linkedList.Count > 0 && linkedList.Last.Value < n){ + linkedList.RemoveLast(); + } + linkedList.AddLast(n); + } + + public int Max(){ + return linkedList.First.Value; + } + + public void Dequeue(int n){ + if(linkedList.First.Value == n){ + linkedList.RemoveFirst(); + } + } + } + + myDequeue window = new myDequeue(); + List res = new List(); + + public int[] MaxSlidingWindow(int[] nums, int k) { + for(int i = 0; i < k; i++){ + window.Enqueue(nums[i]); + } + res.Add(window.Max()); + for(int i = k; i < nums.Length; i++){ + window.Dequeue(nums[i-k]); + window.Enqueue(nums[i]); + res.Add(window.Max()); + } + + return res.ToArray(); + } +``` + -----------------------
diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 1d6a358b..c570672f 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -374,7 +374,42 @@ function topKFrequent(nums: number[], k: number): number[] { }; ``` +C#: +```csharp + public int[] TopKFrequent(int[] nums, int k) { + //哈希表-标权重 + Dictionary dic = new(); + for(int i = 0; i < nums.Length; i++){ + if(dic.ContainsKey(nums[i])){ + dic[nums[i]]++; + }else{ + dic.Add(nums[i], 1); + } + } + //优先队列-从小到大排列 + PriorityQueue pq = new(); + foreach(var num in dic){ + pq.Enqueue(num.Key, num.Value); + if(pq.Count > k){ + pq.Dequeue(); + } + } + + // //Stack-倒置 + // Stack res = new(); + // while(pq.Count > 0){ + // res.Push(pq.Dequeue()); + // } + // return res.ToArray(); + //数组倒装 + int[] res = new int[k]; + for(int i = k - 1; i >= 0; i--){ + res[i] = pq.Dequeue(); + } + return res; + } +``` -----------------------
diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 638c8f4e..9237acdb 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -375,5 +375,22 @@ func removeDuplicates(_ s: String) -> String { } ``` +C#: +```csharp +public string RemoveDuplicates(string s) { + //拿字符串直接作为栈,省去了栈还要转为字符串的操作 + StringBuilder res = new StringBuilder(); + + foreach(char c in s){ + if(res.Length > 0 && res[res.Length-1] == c){ + res.Remove(res.Length-1, 1); + }else{ + res.Append(c); + } + } + + return res.ToString(); + } +``` -----------------------
From c6db9037901601bd84ed907455cbe2adf9f2e9b9 Mon Sep 17 00:00:00 2001 From: White Dove <43168562+zhouweiwei18@users.noreply.github.com> Date: Tue, 28 Jun 2022 16:24:27 +0800 Subject: [PATCH 32/61] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 细节 --- problems/0494.目标和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 8f116fae..00e4bdfa 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -156,7 +156,7 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 有哪些来源可以推出dp[j]呢? -不考虑nums[i]的情况下,填满容量为j - nums[i]的背包,有dp[j - nums[i]]种方法。 +不考虑nums[i]的情况下,填满容量为j的背包,有dp[j]种方法。 那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。 From c70316d7aa59caffe1a0cec9b1c818fcc867a411 Mon Sep 17 00:00:00 2001 From: wzqwtt <1722249371@qq.com> Date: Tue, 28 Jun 2022 21:30:49 +0800 Subject: [PATCH 33/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E8=83=8C=E5=8C=85.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础完全背包.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 54e772e0..fc4609a6 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -359,7 +359,27 @@ function test_CompletePack(): void { test_CompletePack(); ``` +Scala: +```scala +// 先遍历物品,再遍历背包容量 +object Solution { + def test_CompletePack() { + var weight = Array[Int](1, 3, 4) + var value = Array[Int](15, 20, 30) + var baseweight = 4 + + var dp = new Array[Int](baseweight + 1) + + for (i <- 0 until weight.length) { + for (j <- weight(i) to baseweight) { + dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i)) + } + } + dp(baseweight) + } +} +``` ----------------------- From bdd4c83b642dfa5b2c7c5a6b6fd214aa8e0488bb Mon Sep 17 00:00:00 2001 From: wzqwtt <1722249371@qq.com> Date: Tue, 28 Jun 2022 21:56:42 +0800 Subject: [PATCH 34/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200518.=E9=9B=B6?= =?UTF-8?q?=E9=92=B1=E5=85=91=E6=8D=A2II.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0518.零钱兑换II.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 222a10d7..3abb9601 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -289,7 +289,22 @@ function change(amount: number, coins: number[]): number { }; ``` +Scala: +```scala +object Solution { + def change(amount: Int, coins: Array[Int]): Int = { + var dp = new Array[Int](amount + 1) + dp(0) = 1 + for (i <- 0 until coins.length) { + for (j <- coins(i) to amount) { + dp(j) += dp(j - coins(i)) + } + } + dp(amount) + } +} +``` -----------------------
From b982b79830e6bd2d84a7556660139855d153661a Mon Sep 17 00:00:00 2001 From: coderwei <916606569@qq.com> Date: Tue, 5 Jul 2022 15:36:27 +0800 Subject: [PATCH 35/61] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E7=AC=AC111?= =?UTF-8?q?=E9=A2=98=E4=BA=8C=E5=8F=89=E6=A0=91=E6=9C=80=E5=B0=8F=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6js=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 0137bd15..6378300c 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -372,7 +372,7 @@ var minDepth1 = function(root) { // 到叶子节点 返回 1 if(!root.left && !root.right) return 1; // 只有右节点时 递归右节点 - if(!root.left) return 1 + minDepth(root.right);、 + if(!root.left) return 1 + minDepth(root.right); // 只有左节点时 递归左节点 if(!root.right) return 1 + minDepth(root.left); return Math.min(minDepth(root.left), minDepth(root.right)) + 1; From 0e480c58f0f7cc66e77f09cac6231d049dc7f23f Mon Sep 17 00:00:00 2001 From: coderwei <916606569@qq.com> Date: Tue, 5 Jul 2022 18:14:30 +0800 Subject: [PATCH 36/61] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E0234ts=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0234.回文链表.md | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index f591fcef..fa31dede 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -319,6 +319,74 @@ var isPalindrome = function(head) { }; ``` +```typescript +function isPalindrome(head: ListNode | null): boolean { + // 方法1:利用栈结构 将所有元素添加到栈中 + // 声明栈 + let stack:number[] = [] + + // 保存一份头结点 后续需要移动头结点 + let dummy:ListNode = head + + // 遍历链表将所有元素添加到数栈中 + while(dummy){ + stack.push(dummy.val) + dummy = dummy.next + } + + // 利用栈结构的先进先出后出的特性判断是否会文 + while(head){ + if(head.val!== stack.pop()) return false + head = head.next + } + + return true + + // 方法2:翻转后半部分链表 + // 封装翻转链表函数 + function reverseList(node:ListNode){ + let pre:ListNode = null + while(node){ + // 借助第三个变量 进行翻转 有疑问可以看链表篇(https://www.programmercarl.com/链表理论基础.html#单链表) + let temp:ListNode = node.next + node.next = pre + pre = node + node = temp + } + return pre + } + + // 快慢指针 需要注意的我们需要保证slow指针指向在要分隔节点的上一个节点,因为是单链表,无法找到链表前面的节点(也可以使用js代码的方法,新增个变量用去记录前一个节点) + let fast:ListNode = head.next + let slow:ListNode = head + while(fast){ + fast = fast.next + //只有fast有值的情况下才继续移动 不然代码会出现异常 + if(fast) { + fast = fast.next + slow = slow.next + } + } + + // 分隔链表 + let list2Head:ListNode = slow.next + // 断开slow节点的next指针 + slow.next = null + // 将后半部分进行翻转 + let list2 = reverseList(list2Head) + + // 最后进行迭代对比节点的值即可 + while(head && list2){ + if(head.val != list2.val) return false + head = head.next + list2 = list2.next + } + + return true +}; + +``` + ----------------------- From 0331eaa57f39c3a791603b94c1615e6118ac6737 Mon Sep 17 00:00:00 2001 From: wang2jun <91008685+wang2jun@users.noreply.github.com> Date: Tue, 5 Jul 2022 22:43:35 +0800 Subject: [PATCH 37/61] =?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 更正对构造函数的使用 --- problems/0674.最长连续递增序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 5865a68d..7e4d0c19 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -300,7 +300,7 @@ Javascript: > 动态规划: ```javascript const findLengthOfLCIS = (nums) => { - let dp = Array(nums.length).fill(1); + let dp = new Array(nums.length).fill(1); for(let i = 0; i < nums.length - 1; i++) { From 2b68bee8f596b41a6be0b8e458b9ef90b245edcb Mon Sep 17 00:00:00 2001 From: wang2jun <91008685+wang2jun@users.noreply.github.com> Date: Wed, 6 Jul 2022 11:13:36 +0800 Subject: [PATCH 38/61] =?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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index fc7e6f39..0e02e721 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -205,7 +205,7 @@ func min(a, b int) int { Javascript: ```javascript const minDistance = (word1, word2) => { - let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0)); + let dp = Array.from(new Array(word1.length + 1), () => Array(word2.length+1).fill(0)); for(let i = 1; i <= word1.length; i++) { dp[i][0] = i; From c383c1effeacf26ddbbee78e1e2fb29c7034f946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com> Date: Wed, 6 Jul 2022 17:38:33 +0800 Subject: [PATCH 39/61] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python中不用删除节点,这样逻辑更清晰简洁 --- .../0450.删除二叉搜索树中的节点.md | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 3fa2a1c5..81da018f 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -322,33 +322,25 @@ class Solution { ```python class Solution: - def deleteNode(self, root: TreeNode, key: int) -> TreeNode: - if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了 - if root.val == key: - if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - del root - return None - if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - tmp = root - root = root.right - del tmp - return root - if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - tmp = root - root = root.left - del tmp - return root - else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 - v = root.right - while v.left: - v = v.left - v.left = root.left - tmp = root - root = root.right - del tmp - return root - if root.val > key: root.left = self.deleteNode(root.left,key) #左递归 - if root.val < key: root.right = self.deleteNode(root.right,key) #右递归 + def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: + if not root : return None # 节点为空,返回 + if root.val < key : + root.right = self.deleteNode(root.right, key) + elif root.val > key : + root.left = self.deleteNode(root.left, key) + else: + # 当前节点的左子树为空,返回当前的右子树 + if not root.left : return root.right + # 当前节点的右子树为空,返回当前的左子树 + if not root.right: return root.left + # 左右子树都不为空,找到右孩子的最左节点 记为p + node = root.right + while node.left : + node = node.left + # 将当前节点的左子树挂在p的左孩子上 + node.left = root.left + # 当前节点的右子树替换掉当前节点,完成当前节点的删除 + root = root.right return root ``` From eb3eb336dedfcbbcc0d90eab392e4799479d675e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= <20304773@qq.com> Date: Tue, 12 Jul 2022 14:09:05 +0800 Subject: [PATCH 40/61] =?UTF-8?q?Update=200059.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0059.螺旋矩阵II.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index bf0a279e..9690abb6 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -598,5 +598,30 @@ object Solution { } } ``` +C#: +```csharp +public class Solution { + public int[][] GenerateMatrix(int n) { + int[][] answer = new int[n][]; + for(int i = 0; i < n; i++) + answer[i] = new int[n]; + int start = 0; + int end = n - 1; + int tmp = 1; + while(tmp < n * n) + { + for(int i = start; i < end; i++) answer[start][i] = tmp++; + for(int i = start; i < end; i++) answer[i][end] = tmp++; + for(int i = end; i > start; i--) answer[end][i] = tmp++; + for(int i = end; i > start; i--) answer[i][start] = tmp++; + start++; + end--; + } + if(n % 2 == 1) answer[n / 2][n / 2] = tmp; + return answer; + } +} +``` + -----------------------
From f5e1834439d64d09b2e8d8b4e5141510e205dbc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=9D=A8?= <2979742951@qq.com> Date: Wed, 13 Jul 2022 16:15:12 +0800 Subject: [PATCH 41/61] =?UTF-8?q?=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2II=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80=E7=A7=8DC++?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0541.反转字符串II.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 84061ef5..d9b9466c 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -63,6 +63,24 @@ public: }; ``` + +``` +class Solution { +public: + string reverseStr(string s, int k) { + int n=s.size(),pos=0; + while(pos Date: Thu, 14 Jul 2022 10:41:34 +0800 Subject: [PATCH 42/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200090.=E5=AD=90?= =?UTF-8?q?=E9=9B=86II.md=20Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ .idea/encodings.xml | 6 ++ .idea/inspectionProfiles/Project_Default.xml | 70 +++++++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/leetcode-master.iml | 8 +++ .idea/misc.xml | 11 +++ .idea/modules.xml | 8 +++ .idea/vagrant.xml | 7 ++ .idea/vcs.xml | 6 ++ problems/0090.子集II.md | 27 +++++++ 10 files changed, 157 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vagrant.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..35410cac --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..c2bae49d --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..3f19e2d8 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,70 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 00000000..d0876a78 --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..8ca8e3b1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..7c250acd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vagrant.xml b/.idea/vagrant.xml new file mode 100644 index 00000000..a5aa7868 --- /dev/null +++ b/.idea/vagrant.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index e85ec66d..d503846e 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -261,6 +261,33 @@ class Solution: self.path.pop() ``` +### Python3 +```python3 +class Solution: + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: + res = [] + path = [] + nums.sort() # 去重需要先对数组进行排序 + + def backtracking(nums, startIndex): + # 终止条件 + res.append(path[:]) + if startIndex == len(nums): + return + + # for循环 + for i in range(startIndex, len(nums)): + # 数层去重 + if i > startIndex and nums[i] == nums[i-1]: # 去重 + continue + path.append(nums[i]) + backtracking(nums, i+1) + path.pop() + + backtracking(nums, 0) + return res +``` + ### Go ```Go From 57840efc1a647803b24b5c793f4622c7e150721f Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Fri, 15 Jul 2022 20:06:53 +0800 Subject: [PATCH 43/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C=200040.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8CII=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0039.组合总和 0040.组合总和II Rust版本 --- problems/0039.组合总和.md | 29 +++++++++++++++++++++++++++ problems/0040.组合总和II.md | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index 564d13ea..0f9cbc23 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -417,6 +417,35 @@ function combinationSum(candidates: number[], target: number): number[][] { }; ``` +## Rust + +```Rust +impl Solution { + pub fn backtracking(result: &mut Vec>, path: &mut Vec, candidates: &Vec, target: i32, mut sum: i32, start_index: usize) { + if sum == target { + result.push(path.to_vec()); + return; + } + for i in start_index..candidates.len() { + if sum + candidates[i] <= target { + sum += candidates[i]; + path.push(candidates[i]); + Self::backtracking(result, path, candidates, target, sum, i); + sum -= candidates[i]; + path.pop(); + } + } + } + + pub fn combination_sum(candidates: Vec, target: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, &candidates, target, 0, 0); + result + } +} +``` + ## C ```c diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 557eb855..99577f0c 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -599,6 +599,41 @@ function combinationSum2(candidates: number[], target: number): number[][] { }; ``` +## Rust + +```Rust +impl Solution { + pub fn backtracking(result: &mut Vec>, path: &mut Vec, candidates: &Vec, target: i32, mut sum: i32, start_index: usize, used: &mut Vec) { + if sum == target { + result.push(path.to_vec()); + return; + } + for i in start_index..candidates.len() { + if sum + candidates[i] <= target { + if i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false { continue; } + sum += candidates[i]; + path.push(candidates[i]); + used[i] = true; + Self::backtracking(result, path, candidates, target, sum, i + 1, used); + used[i] = false; + sum -= candidates[i]; + path.pop(); + } + } + } + + pub fn combination_sum2(candidates: Vec, target: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + let mut used: Vec = vec![false; candidates.len()]; + let mut candidates = candidates; + candidates.sort(); + Self::backtracking(&mut result, &mut path, &candidates, target, 0, 0, &mut used); + result + } +} +``` + ## C ```c From fb982fa79d643bb3e01960bb01897322cc424bf7 Mon Sep 17 00:00:00 2001 From: xiaojun <13589818805@163.com> Date: Sat, 16 Jul 2022 15:00:22 +0800 Subject: [PATCH 44/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88234.=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E9=93=BE=E8=A1=A8=EF=BC=89=E7=9A=84go=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0234.回文链表.md | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index eeee6fa5..1ac8756a 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -276,7 +276,75 @@ class Solution: ### Go ```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +//方法一,使用数组 +func isPalindrome(head *ListNode) bool{ + //计算切片长度,避免切片频繁扩容 + cur,ln:=head,0 + for cur!=nil{ + ln++ + cur=cur.Next + } + nums:=make([]int,ln) + index:=0 + for head!=nil{ + nums[index]=head.Val + index++ + head=head.Next + } + //比较回文切片 + for i,j:=0,ln-1;i<=j;i,j=i+1,j-1{ + if nums[i]!=nums[j]{return false} + } + return true +} +// 方法二,快慢指针 +func isPalindrome(head *ListNode) bool { + if head==nil&&head.Next==nil{return true} + //慢指针,找到链表中间分位置,作为分割 + slow:=head + fast:=head + //记录慢指针的前一个节点,用来分割链表 + pre:=head + for fast!=nil && fast.Next!=nil{ + pre=slow + slow=slow.Next + fast=fast.Next.Next + } + //分割链表 + pre.Next=nil + //前半部分 + cur1:=head + //反转后半部分,总链表长度如果是奇数,cur2比cur1多一个节点 + cur2:=ReverseList(slow) + + //开始两个链表的比较 + for cur1!=nil{ + if cur1.Val!=cur2.Val{return false} + cur1=cur1.Next + cur2=cur2.Next + } + return true +} +//反转链表 +func ReverseList(head *ListNode) *ListNode{ + var pre *ListNode + cur:=head + for cur!=nil{ + tmp:=cur.Next + cur.Next=pre + pre=cur + cur=tmp + } + return pre +} ``` ### JavaScript From 4f867df86c7ef1ba3ef2ae3858fc1f928025a070 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Sat, 16 Jul 2022 15:24:05 +0800 Subject: [PATCH 45/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20Rust=E5=9B=9E=E6=BA=AF+?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0131.分割回文串 Rust回溯+动态规划版本 --- problems/0131.分割回文串.md | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 64d45853..a54d6576 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -626,7 +626,8 @@ func partition(_ s: String) -> [[String]] { ## Rust -```rust +**回溯+函数判断回文串** +```Rust impl Solution { pub fn partition(s: String) -> Vec> { let mut ret = vec![]; @@ -676,6 +677,40 @@ impl Solution { } } ``` +**回溯+动态规划预处理判断回文串** +```Rust +impl Solution { + pub fn backtracking(is_palindrome: &Vec>, result: &mut Vec>, path: &mut Vec, s: &Vec, start_index: usize) { + let len = s.len(); + if start_index >= len { + result.push(path.to_vec()); + return; + } + for i in start_index..len { + if is_palindrome[start_index][i] { path.push(s[start_index..=i].iter().collect::()); } else { continue; } + Self::backtracking(is_palindrome, result, path, s, i + 1); + path.pop(); + } + } + + pub fn partition(s: String) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + let s = s.chars().collect::>(); + let len: usize = s.len(); + // 使用动态规划预先打表 + // 当且仅当其为空串(i>j),或其长度为1(i=j),或者首尾字符相同且(s[i+1..j−1])时为回文串 + let mut is_palindrome = vec![vec![true; len]; len]; + for i in (0..len).rev() { + for j in (i + 1)..len { + is_palindrome[i][j] = s[i] == s[j] && is_palindrome[i + 1][j - 1]; + } + } + Self::backtracking(&is_palindrome, &mut result, &mut path, &s, 0); + result + } +} +``` ## Scala From bafb7c45db1b9975c590ec93a65034c7b2190656 Mon Sep 17 00:00:00 2001 From: lizhendong128 Date: Thu, 21 Jul 2022 19:50:02 +0800 Subject: [PATCH 46/61] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200309.=E6=9C=80?= =?UTF-8?q?=E4=BD=B3=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E5=86=B7=E5=86=BB=E6=9C=9F=20=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E7=9A=84=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0309.最佳买卖股票时机含冷冻期.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 229fc636..3791a71f 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -44,6 +44,8 @@ dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]。 * 状态三:今天卖出了股票 * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! +![](https://img-blog.csdnimg.cn/518d5baaf33f4b2698064f8efb42edbf.png) + j的状态为: * 0:状态一 @@ -57,7 +59,7 @@ j的状态为: **注意这里的每一个状态,例如状态一,是买入股票状态并不是说今天已经就买入股票,而是说保存买入股票的状态即:可能是前几天买入的,之后一直没操作,所以保持买入股票的状态**。 -2. 确定递推公式 +1. 确定递推公式 达到买入股票状态(状态一)即:dp[i][0],有两个具体操作: From 8865ed851aeb14a7e768c9fa5d7ef0c2a110c56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= <20304773@qq.com> Date: Fri, 22 Jul 2022 15:36:23 +0800 Subject: [PATCH 47/61] =?UTF-8?q?Update=200941.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=B1=B1=E8=84=89=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0941.有效的山脉数组.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index 310dd35a..fb7935a8 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -177,7 +177,24 @@ function validMountainArray(arr: number[]): boolean { }; ``` +## C# +```csharp +public class Solution { + public bool ValidMountainArray(int[] arr) { + if (arr.Length < 3) return false; + + int left = 0; + int right = arr.Length - 1; + + while (left + 1< arr.Length && arr[left] < arr[left + 1]) left ++; + while (right > 0 && arr[right] < arr[right - 1]) right --; + if (left == right && left != 0 && right != arr.Length - 1) return true; + + return false; + } +} +``` ----------------------- From f5b44d5f8c8ed2136a396a104b4f62884319eede Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Sat, 23 Jul 2022 13:06:47 +0800 Subject: [PATCH 48/61] =?UTF-8?q?js=20=E4=BB=A3=E7=A0=81=E6=97=A0=E9=AB=98?= =?UTF-8?q?=E4=BA=AE=E6=8F=90=E7=A4=BA?= 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 cbfe046a..c7bf24bc 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -418,7 +418,7 @@ function combinationSum2(candidates, target) { **47. 全排列 II** -```javaescript +```javascript function permuteUnique(nums) { const resArr = []; const usedArr = []; From fdf0185e1d53e8cda56a6cb040da5b6218fbb3c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 26 Jul 2022 09:17:40 +0800 Subject: [PATCH 49/61] =?UTF-8?q?Update=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0541.反转字符串II.md | 34 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index d9b9466c..8c654a17 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -64,22 +64,7 @@ public: ``` -``` -class Solution { -public: - string reverseStr(string s, int k) { - int n=s.size(),pos=0; - while(pos Date: Sat, 30 Jul 2022 09:45:38 +0800 Subject: [PATCH 50/61] =?UTF-8?q?0452.=E7=94=A8=E6=9C=80=E5=B0=91=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86=E6=B0=94=E7=90=83?= =?UTF-8?q?=20Java=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0452.用最少数量的箭引爆气球.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index 3042b063..7b5f387d 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -151,6 +151,7 @@ class Solution { //重叠气球的最小右边界 int leftmostRightBound = points[0][1]; //如果下一个气球的左边界大于最小右边界 + for(int i = 1; i < points.length; i++){ if (points[i][0] > leftmostRightBound ) { //增加一次射击 count++; From 9343ddd30cb63ad51db6f023c581f31b1f3f9d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Mon, 1 Aug 2022 10:50:27 +0800 Subject: [PATCH 51/61] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 00e4bdfa..cb1b9bd1 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -158,7 +158,7 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 不考虑nums[i]的情况下,填满容量为j的背包,有dp[j]种方法。 -那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。 +那么考虑nums[i]的话(只要搞到nums[i]),凑成dp[j]就有dp[j - nums[i]] 种方法。 例如:dp[j],j 为5, From 1bfd42e702b997caf5563b1823be4067cd6ad0a6 Mon Sep 17 00:00:00 2001 From: Invalided <39646604+Invalided@users.noreply.github.com> Date: Mon, 1 Aug 2022 12:31:24 +0800 Subject: [PATCH 52/61] =?UTF-8?q?Update=200031.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0031.下一个排列Java版本的优化代码,时间复杂度为O(N),空间复杂度O(1) --- problems/0031.下一个排列.md | 46 ++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 1a3641b0..ada9deba 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -116,6 +116,48 @@ class Solution { } } ``` +> 优化时间复杂度为O(N),空间复杂度为O(1) +```Java +class Solution { + public void nextPermutation(int[] nums) { + // 1.从后向前获取逆序区域的前一位 + int index = findIndex(nums); + // 判断数组是否处于最小组合状态 + if(index != 0){ + // 2.交换逆序区域刚好大于它的最小数字 + exchange(nums,index); + } + // 3.把原来的逆序区转为顺序 + reverse(nums,index); + } + + public static int findIndex(int [] nums){ + for(int i = nums.length-1;i>0;i--){ + if(nums[i]>nums[i-1]){ + return i; + } + } + return 0; + } + public static void exchange(int [] nums, int index){ + int head = nums[index-1]; + for(int i = nums.length-1;i>0;i--){ + if(head < nums[i]){ + nums[index-1] = nums[i]; + nums[i] = head; + break; + } + } + } + public static void reverse(int [] nums, int index){ + for(int i = index,j = nums.length-1;i直接使用sorted()不符合题意 @@ -164,7 +206,7 @@ class Solution: high -= 1 ``` >上一版本简化版 -'''python +```python class Solution(object): def nextPermutation(self, nums: List[int]) -> None: n = len(nums) @@ -185,7 +227,7 @@ class Solution(object): end -= 1 return nums -''' +``` ## Go From cb01b142fb04123a4debfbe8a2e1950fb13b3479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:34:28 +0800 Subject: [PATCH 53/61] Delete .gitignore --- .idea/.gitignore | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410cac..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml From 5d690afae6e501fcdf0385f23bb81ddc633c4bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:34:34 +0800 Subject: [PATCH 54/61] Delete encodings.xml --- .idea/encodings.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/encodings.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index c2bae49d..00000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 52c395702c37d28f459c0076279ef0e34bad6f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:34:41 +0800 Subject: [PATCH 55/61] Delete profiles_settings.xml --- .idea/inspectionProfiles/profiles_settings.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2da..00000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file From 0f042a29cc62efe0cbdb4c69f717e1800f72e561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:34:47 +0800 Subject: [PATCH 56/61] Delete modules.xml --- .idea/modules.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/modules.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acd..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From 8b10e7473188a6dd5922566239049364cfa8a1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:35:00 +0800 Subject: [PATCH 57/61] Delete vagrant.xml --- .idea/vagrant.xml | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .idea/vagrant.xml diff --git a/.idea/vagrant.xml b/.idea/vagrant.xml deleted file mode 100644 index a5aa7868..00000000 --- a/.idea/vagrant.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - \ No newline at end of file From ea9f7c61392b8902aa3844ffcfdf4b360c5f609a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:35:07 +0800 Subject: [PATCH 58/61] Delete vcs.xml --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 227f8ee98d4b3ad5a713488bdd4435d2e784c154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:35:20 +0800 Subject: [PATCH 59/61] Delete Project_Default.xml --- .idea/inspectionProfiles/Project_Default.xml | 70 -------------------- 1 file changed, 70 deletions(-) delete mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 3f19e2d8..00000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,70 +0,0 @@ - - - - \ No newline at end of file From d49ca8f278f57831b9410d9a419845b199dcb395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:35:28 +0800 Subject: [PATCH 60/61] Delete leetcode-master.iml --- .idea/leetcode-master.iml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/leetcode-master.iml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index d0876a78..00000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From 2a4a210b45d9ea8b67dc19e18591cb622941d7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 2 Aug 2022 09:35:35 +0800 Subject: [PATCH 61/61] Delete misc.xml --- .idea/misc.xml | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 .idea/misc.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 8ca8e3b1..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - \ No newline at end of file