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 a8e4d4d

Browse files
committed
添加 0093.复原IP地址.md Scala版本
1 parent c0a73e2 commit a8e4d4d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎problems/0093.复原IP地址.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,48 @@ func restoreIpAddresses(_ s: String) -> [String] {
659659
}
660660
```
661661

662+
## Scala
663+
664+
```scala
665+
object Solution {
666+
import scala.collection.mutable
667+
def restoreIpAddresses(s: String): List[String] = {
668+
var result = mutable.ListBuffer[String]()
669+
if (s.size < 4 || s.length > 12) return result.toList
670+
var path = mutable.ListBuffer[String]()
671+
672+
// 判断IP中的一个字段是否为正确的
673+
def isIP(sub: String): Boolean = {
674+
if (sub.size > 1 && sub(0) == '0') return false
675+
if (sub.toInt > 255) return false
676+
true
677+
}
678+
679+
def backtracking(startIndex: Int): Unit = {
680+
if (startIndex >= s.size) {
681+
if (path.size == 4) {
682+
result.append(path.mkString(".")) // mkString方法可以把集合里的数据以指定字符串拼接
683+
return
684+
}
685+
return
686+
}
687+
// subString
688+
for (i <- startIndex until startIndex + 3 if i < s.size) {
689+
var subString = s.substring(startIndex, i + 1)
690+
if (isIP(subString)) { // 如果合法则进行下一轮
691+
path.append(subString)
692+
backtracking(i + 1)
693+
path = path.take(path.size - 1)
694+
}
695+
}
696+
}
697+
698+
backtracking(0)
699+
result.toList
700+
}
701+
}
702+
```
703+
662704

663705
-----------------------
664706
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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