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 94350c0

Browse files
committed
添加 0763.划分字母区间.md Scala版本
1 parent ece2c3e commit 94350c0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/0763.划分字母区间.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,31 @@ function partitionLabels(s: string): number[] {
317317
};
318318
```
319319

320+
### Scala
321+
322+
```scala
323+
object Solution {
324+
import scala.collection.mutable
325+
def partitionLabels(s: String): List[Int] = {
326+
var hash = new Array[Int](26)
327+
for (i <- s.indices) {
328+
hash(s(i) - 'a') = i
329+
}
330+
331+
var res = mutable.ListBuffer[Int]()
332+
var (left, right) = (0, 0)
333+
for (i <- s.indices) {
334+
right = math.max(hash(s(i) - 'a'), right)
335+
if (i == right) {
336+
res.append(right - left + 1)
337+
left = i + 1
338+
}
339+
}
320340

341+
res.toList
342+
}
343+
}
344+
```
321345

322346

323347
-----------------------

0 commit comments

Comments
(0)

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