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 1/3] =?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 2/3] =?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 3/3] =?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 + } +} +``` -----------------------