mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1329 from wzqwtt/patch10
添加(0344.反转字符串、0541.反转字符串II、剑指Offer05.替换空格)Scala版本
This commit is contained in:
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user