mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加 0763.划分字母区间.md Scala版本
This commit is contained in:
@ -317,7 +317,31 @@ function partitionLabels(s: string): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def partitionLabels(s: String): List[Int] = {
|
||||||
|
var hash = new Array[Int](26)
|
||||||
|
for (i <- s.indices) {
|
||||||
|
hash(s(i) - 'a') = i
|
||||||
|
}
|
||||||
|
|
||||||
|
var res = mutable.ListBuffer[Int]()
|
||||||
|
var (left, right) = (0, 0)
|
||||||
|
for (i <- s.indices) {
|
||||||
|
right = math.max(hash(s(i) - 'a'), right)
|
||||||
|
if (i == right) {
|
||||||
|
res.append(right - left + 1)
|
||||||
|
left = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user