Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7d356f

Browse files
committed
添加 剑指Offer58-II.左旋转字符串.md Scala版本
1 parent 78b367e commit c7d356f

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

‎problems/剑指Offer58-II.左旋转字符串.md‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,34 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
290290
}
291291
```
292292

293-
293+
Scala:
294+
295+
```scala
296+
object Solution {
297+
def reverseLeftWords(s: String, n: Int): String = {
298+
var str = s.toCharArray // 转换为Array
299+
// abcdefg => ba cdefg
300+
reverseString(str, 0, n - 1)
301+
// ba cdefg => ba gfedc
302+
reverseString(str, n, str.length - 1)
303+
// ba gfedc => cdefgab
304+
reverseString(str, 0, str.length - 1)
305+
// 最终返回,return关键字可以省略
306+
new String(str)
307+
}
308+
// 翻转字符串
309+
def reverseString(s: Array[Char], start: Int, end: Int): Unit = {
310+
var (left, right) = (start, end)
311+
while (left < right) {
312+
var tmp = s(left)
313+
s(left) = s(right)
314+
s(right) = tmp
315+
left += 1
316+
right -= 1
317+
}
318+
}
319+
}
320+
```
294321

295322

296323

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /