From e4da60add91e39177250f6808c3846c9b563612a Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 15:44:42 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200344.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.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/0344.反转字符串.md | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md
index 58bada05..e1f27bd7 100644
--- a/problems/0344.反转字符串.md
+++ b/problems/0344.反转字符串.md
@@ -266,6 +266,20 @@ public class Solution
}
}
```
-
+Scala:
+```scala
+object Solution {
+ def reverseString(s: Array[Char]): Unit = {
+ var (left, right) = (0, s.length - 1)
+ while (left < right) {
+ var tmp = s(left)
+ s(left) = s(right)
+ s(right) = tmp
+ left += 1
+ right -= 1
+ }
+ }
+}
+```
-----------------------
From f2dcdbe94757d3ef88278e4f8c80281ce7baf5c9 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 16:32:23 +0800
Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200541.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2II.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/0541.反转字符串II.md | 41 ++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md
index 8c13a390..99d6ebe0 100644
--- a/problems/0541.反转字符串II.md
+++ b/problems/0541.反转字符串II.md
@@ -347,6 +347,47 @@ public class Solution
}
}
```
+Scala:
+版本一: (正常解法)
+```scala
+object Solution {
+ def reverseStr(s: String, k: Int): String = {
+ val res = s.toCharArray // 转换为Array好处理
+ for (i <- s.indices by 2 * k) {
+ // 如果i+k大于了res的长度,则需要全部翻转
+ if (i + k > res.length) {
+ reverse(res, i, s.length - 1)
+ } else {
+ reverse(res, i, i + k - 1)
+ }
+ }
+ new String(res)
+ }
+ // 翻转字符串,从start到end
+ def reverse(s: Array[Char], start: Int, end: Int): Unit = {
+ var (left, right) = (start, end)
+ while (left < right) {
+ var tmp = s(left)
+ s(left) = s(right)
+ s(right) = tmp
+ left += 1
+ right -= 1
+ }
+ }
+}
+```
+版本二: 首先利用sliding每隔k个进行分割,随后转换为数组,再使用zipWithIndex添加每个数组的索引,紧接着利用map做变换,如果索引%2==0则说明需要翻转,否则原封不动,最后再转换为String
+```scala
+object Solution {
+ def reverseStr(s: String, k: Int): String = {
+ s.sliding(k, k)
+ .toArray
+ .zipWithIndex
+ .map(v => if (v._2 % 2 == 0) v._1.reverse else v._1)
+ .mkString
+ }
+}
+```
-----------------------
From 037bebbe90eb7bd607314d5d3d5d63e506f5aeeb Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 17:04:40 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87Offe?=
=?UTF-8?q?r05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.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/剑指Offer05.替换空格.md | 56 +++++++++++++++++++++++++-
1 file changed, 55 insertions(+), 1 deletion(-)
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md
index 037bd427..eecd7f0c 100644
--- a/problems/剑指Offer05.替换空格.md
+++ b/problems/剑指Offer05.替换空格.md
@@ -413,8 +413,62 @@ func replaceSpace(_ s: String) -> String {
}
```
+Scala:
-
+方式一: 双指针
+```scala
+object Solution {
+ def replaceSpace(s: String): String = {
+ var count = 0
+ s.foreach(c => if (c == ' ') count += 1) // 统计空格的数量
+ val sOldSize = s.length // 旧数组字符串长度
+ val sNewSize = s.length + count * 2 // 新数组字符串长度
+ val res = new Array[Char](sNewSize) // 新数组
+ var index = sNewSize - 1 // 新数组索引
+ // 逆序遍历
+ for (i <- (0 until sOldSize).reverse) {
+ if (s(i) == ' ') {
+ res(index) = '0'
+ index -= 1
+ res(index) = '2'
+ index -= 1
+ res(index) = '%'
+ } else {
+ res(index) = s(i)
+ }
+ index -= 1
+ }
+ res.mkString
+ }
+}
+```
+方式二: 使用一个集合,遇到空格就添加%20
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def replaceSpace(s: String): String = {
+ val res: ListBuffer[Char] = ListBuffer[Char]()
+ for (i <- s.indices) {
+ if (s(i) == ' ') {
+ res += '%'
+ res += '2'
+ res += '0'
+ }else{
+ res += s(i)
+ }
+ }
+ res.mkString
+ }
+}
+```
+方式三: 使用map
+```scala
+object Solution {
+ def replaceSpace(s: String): String = {
+ s.map(c => if(c == ' ') "%20" else c).mkString
+ }
+}
+```
-----------------------