File tree Expand file tree Collapse file tree 3 files changed +112
-3
lines changed Expand file tree Collapse file tree 3 files changed +112
-3
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,20 @@ public class Solution
266
266
}
267
267
}
268
268
```
269
-
269
+ Scala:
270
+ ``` scala
271
+ object Solution {
272
+ def reverseString (s : Array [Char ]): Unit = {
273
+ var (left, right) = (0 , s.length - 1 )
274
+ while (left < right) {
275
+ var tmp = s(left)
276
+ s(left) = s(right)
277
+ s(right) = tmp
278
+ left += 1
279
+ right -= 1
280
+ }
281
+ }
282
+ }
283
+ ```
270
284
-----------------------
271
285
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
Original file line number Diff line number Diff line change @@ -347,6 +347,47 @@ public class Solution
347
347
}
348
348
}
349
349
```
350
-
350
+ Scala:
351
+
352
+ 版本一: (正常解法)
353
+ ``` scala
354
+ object Solution {
355
+ def reverseStr (s : String , k : Int ): String = {
356
+ val res = s.toCharArray // 转换为Array好处理
357
+ for (i <- s.indices by 2 * k) {
358
+ // 如果i+k大于了res的长度,则需要全部翻转
359
+ if (i + k > res.length) {
360
+ reverse(res, i, s.length - 1 )
361
+ } else {
362
+ reverse(res, i, i + k - 1 )
363
+ }
364
+ }
365
+ new String (res)
366
+ }
367
+ // 翻转字符串,从start到end
368
+ def reverse (s : Array [Char ], start : Int , end : Int ): Unit = {
369
+ var (left, right) = (start, end)
370
+ while (left < right) {
371
+ var tmp = s(left)
372
+ s(left) = s(right)
373
+ s(right) = tmp
374
+ left += 1
375
+ right -= 1
376
+ }
377
+ }
378
+ }
379
+ ```
380
+ 版本二: 首先利用sliding每隔k个进行分割,随后转换为数组,再使用zipWithIndex添加每个数组的索引,紧接着利用map做变换,如果索引%2==0则说明需要翻转,否则原封不动,最后再转换为String
381
+ ``` scala
382
+ object Solution {
383
+ def reverseStr (s : String , k : Int ): String = {
384
+ s.sliding(k, k)
385
+ .toArray
386
+ .zipWithIndex
387
+ .map(v => if (v._2 % 2 == 0 ) v._1.reverse else v._1)
388
+ .mkString
389
+ }
390
+ }
391
+ ```
351
392
-----------------------
352
393
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
Original file line number Diff line number Diff line change @@ -413,8 +413,62 @@ func replaceSpace(_ s: String) -> String {
413
413
}
414
414
```
415
415
416
+ Scala:
416
417
417
-
418
+ 方式一: 双指针
419
+ ``` scala
420
+ object Solution {
421
+ def replaceSpace (s : String ): String = {
422
+ var count = 0
423
+ s.foreach(c => if (c == ' ' ) count += 1 ) // 统计空格的数量
424
+ val sOldSize = s.length // 旧数组字符串长度
425
+ val sNewSize = s.length + count * 2 // 新数组字符串长度
426
+ val res = new Array [Char ](sNewSize) // 新数组
427
+ var index = sNewSize - 1 // 新数组索引
428
+ // 逆序遍历
429
+ for (i <- (0 until sOldSize).reverse) {
430
+ if (s(i) == ' ' ) {
431
+ res(index) = '0'
432
+ index -= 1
433
+ res(index) = '2'
434
+ index -= 1
435
+ res(index) = '%'
436
+ } else {
437
+ res(index) = s(i)
438
+ }
439
+ index -= 1
440
+ }
441
+ res.mkString
442
+ }
443
+ }
444
+ ```
445
+ 方式二: 使用一个集合,遇到空格就添加%20
446
+ ``` scala
447
+ object Solution {
448
+ import scala .collection .mutable .ListBuffer
449
+ def replaceSpace (s : String ): String = {
450
+ val res : ListBuffer [Char ] = ListBuffer [Char ]()
451
+ for (i <- s.indices) {
452
+ if (s(i) == ' ' ) {
453
+ res += '%'
454
+ res += '2'
455
+ res += '0'
456
+ }else {
457
+ res += s(i)
458
+ }
459
+ }
460
+ res.mkString
461
+ }
462
+ }
463
+ ```
464
+ 方式三: 使用map
465
+ ``` scala
466
+ object Solution {
467
+ def replaceSpace (s : String ): String = {
468
+ s.map(c => if (c == ' ' ) " %20" else c).mkString
469
+ }
470
+ }
471
+ ```
418
472
419
473
420
474
-----------------------
You can’t perform that action at this time.
0 commit comments