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 e2b638d

Browse files
committed
Solve 352. Data Stream as Disjoint Intervals
1 parent f31b364 commit e2b638d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class SummaryRanges() {
2+
3+
private val valueSet = sortedSetOf<Int>()
4+
5+
fun addNum(value: Int) {
6+
valueSet.add(value)
7+
}
8+
9+
fun getIntervals(): Array<IntArray> {
10+
val values = valueSet.toList()
11+
val intervalList = mutableListOf<List<Int>>()
12+
var start = values[0]
13+
var end = values[0]
14+
15+
for (i in 1 until values.size) {
16+
val value = values[i]
17+
18+
if (value - end == 1) {
19+
end = value
20+
} else {
21+
intervalList.add(listOf(start, end))
22+
start = value
23+
end = value
24+
}
25+
}
26+
27+
intervalList.add(listOf(start, end))
28+
29+
return intervalList.map { it.toIntArray() }.toTypedArray()
30+
}
31+
32+
}
33+
34+
/**
35+
* Your SummaryRanges object will be instantiated and called as such:
36+
* var obj = SummaryRanges()
37+
* obj.addNum(value)
38+
* var param_2 = obj.getIntervals()
39+
*/

0 commit comments

Comments
(0)

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