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 c0a73e2

Browse files
committed
添加 0131.分割回文串.md Scala版本
1 parent 9c32528 commit c0a73e2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎problems/0131.分割回文串.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,5 +676,50 @@ impl Solution {
676676
}
677677
}
678678
```
679+
680+
681+
## Scala
682+
683+
```scala
684+
object Solution {
685+
686+
import scala.collection.mutable
687+
688+
def partition(s: String): List[List[String]] = {
689+
var result = mutable.ListBuffer[List[String]]()
690+
var path = mutable.ListBuffer[String]()
691+
692+
// 判断字符串是否回文
693+
def isPalindrome(start: Int, end: Int): Boolean = {
694+
var (left, right) = (start, end)
695+
while (left < right) {
696+
if (s(left) != s(right)) return false
697+
left += 1
698+
right -= 1
699+
}
700+
true
701+
}
702+
703+
// 回溯算法
704+
def backtracking(startIndex: Int): Unit = {
705+
if (startIndex >= s.size) {
706+
result.append(path.toList)
707+
return
708+
}
709+
// 添加循环守卫,如果当前分割是回文子串则进入回溯
710+
for (i <- startIndex until s.size if isPalindrome(startIndex, i)) {
711+
path.append(s.substring(startIndex, i + 1))
712+
backtracking(i + 1)
713+
path = path.take(path.size - 1)
714+
}
715+
}
716+
717+
backtracking(0)
718+
result.toList
719+
}
720+
}
721+
```
722+
723+
679724
-----------------------
680725
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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