From 18f29ef1786e41fba506bc27f35c6a83a116ba14 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 10 Jun 2022 16:28:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 17db4a85..443ab6d7 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -296,5 +296,26 @@ int findContentChildren(int* g, int gSize, int* s, int sSize){ } ``` +### Scala + +```scala +object Solution { + def findContentChildren(g: Array[Int], s: Array[Int]): Int = { + var result = 0 + var children = g.sorted + var cookie = s.sorted + // 遍历饼干 + var j = 0 + for (i <- cookie.indices) { + if (j < children.size && cookie(i) >= children(j)) { + j += 1 + result += 1 + } + } + result + } +} +``` + -----------------------
From 7fd311f47bf257787caeea3bcbf73ec3ce29e978 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 10 Jun 2022 20:09:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 6822896e..fb4d6eff 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -375,7 +375,31 @@ function wiggleMaxLength(nums: number[]): number { }; ``` +### Scala +```scala +object Solution { + def wiggleMaxLength(nums: Array[Int]): Int = { + if (nums.length <= 1) return nums.length + var result = 1 + var curDiff = 0 // 当前一对的差值 + var preDiff = 0 // 前一对的差值 + + for (i <- 1 until nums.length) { + curDiff = nums(i) - nums(i - 1) // 计算当前这一对的差值 + // 当 curDiff > 0 的情况,preDiff <= 0 + // 当 curDiff < 0 的情况,preDiff >= 0 + // 这两种情况算是两个峰值 + if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) { + result += 1 // 结果集加 1 + preDiff = curDiff // 当前差值赋值给上一轮 + } + } + + result + } +} +``` -----------------------