Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1329 from wzqwtt/patch10
Browse files Browse the repository at this point in the history
添加(0344.反转字符串、0541.反转字符串II、剑指Offer05.替换空格)Scala版本
  • Loading branch information
youngyangyang04 committed Jun 4, 2022
2 parents 83726ac + 037bebb commit dab44f5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
16 changes: 15 additions & 1 deletion problems/0344.反转字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
43 changes: 42 additions & 1 deletion problems/0541.反转字符串II.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
56 changes: 55 additions & 1 deletion problems/剑指Offer05.替换空格.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```


-----------------------
Expand Down

0 comments on commit dab44f5

Please sign in to comment.