Skip to content

Commit d381471

Browse files
committed
Create: 0352-data-stream-as-disjoint-intervals
1 parent ee7a3bc commit d381471

4 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import "sort"
4+
5+
type SummaryRanges struct {
6+
numSet map[int]bool
7+
}
8+
9+
func Constructor() SummaryRanges {
10+
return SummaryRanges{numSet: map[int]bool{}}
11+
}
12+
13+
func (this *SummaryRanges) AddNum(value int) {
14+
this.numSet[value] = true
15+
}
16+
17+
func (this *SummaryRanges) GetIntervals() [][]int {
18+
nums := []int{}
19+
for num, _ := range this.numSet {
20+
nums = append(nums, num)
21+
}
22+
sort.Ints(nums)
23+
24+
res := [][]int{}
25+
26+
i := 0
27+
28+
for i < len(nums) {
29+
start := nums[i]
30+
31+
for i +1 < len(nums) && nums[i] +1 == nums[i+1] {
32+
i++
33+
}
34+
35+
res = append(res, []int{start, nums[i]})
36+
i++
37+
}
38+
39+
return res
40+
}
41+
42+
func main() {
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class SummaryRanges {
2+
constructor() {
3+
this.numSet = new Set();
4+
}
5+
6+
addNum(value) {
7+
this.numSet.add(value);
8+
}
9+
10+
getIntervals() {
11+
let nums = Array.from(this.numSet.keys());
12+
nums.sort((a, b) => a - b);
13+
14+
let res = [];
15+
16+
let i = 0;
17+
18+
while (i < nums.length) {
19+
let start = nums[i];
20+
21+
while (i + 1 < nums.length && nums[i] + 1 == nums[i + 1]) {
22+
i++;
23+
}
24+
25+
res.push([start, nums[i]]);
26+
i++;
27+
}
28+
29+
return res;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// BTreeSet Solution
2+
3+
use std::collections::BTreeSet;
4+
5+
struct SummaryRanges {
6+
tree_map: BTreeSet<i32>,
7+
}
8+
9+
impl SummaryRanges {
10+
fn new() -> Self {
11+
Self {
12+
tree_map: BTreeSet::new(),
13+
}
14+
}
15+
16+
fn add_num(&mut self, value: i32) {
17+
self.tree_map.insert(value);
18+
}
19+
20+
fn get_intervals(&self) -> Vec<Vec<i32>> {
21+
let mut res: Vec<Vec<i32>> = vec![];
22+
23+
for n in &self.tree_map {
24+
if !res.is_empty() && res.last().unwrap()[1] + 1 == *n {
25+
let mut last = res.pop().unwrap();
26+
last[1] = *n;
27+
res.push(last);
28+
} else {
29+
res.push(vec![*n, *n]);
30+
}
31+
}
32+
33+
res
34+
}
35+
}
36+
37+
// HashSet Solution
38+
use std::collections::HashSet;
39+
struct SummaryRanges {
40+
ranges: Vec<Vec<i32>>,
41+
num_set: HashSet<i32>,
42+
}
43+
44+
impl SummaryRanges {
45+
fn new() -> Self {
46+
Self {
47+
ranges: vec![],
48+
num_set: HashSet::new(),
49+
}
50+
}
51+
52+
fn add_num(&mut self, value: i32) {
53+
self.num_set.insert(value);
54+
}
55+
56+
fn get_intervals(&self) -> Vec<Vec<i32>> {
57+
let mut nums: Vec<i32> = self.num_set.iter().cloned().collect();
58+
nums.sort();
59+
let mut res = vec![];
60+
let mut i = 0;
61+
62+
while i < nums.len() {
63+
let start = nums[i];
64+
65+
while i + 1 < nums.len() && nums[i] + 1 == nums[i + 1] {
66+
i += 1;
67+
}
68+
res.push(vec![start, nums[i]]);
69+
i += 1;
70+
}
71+
res
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class SummaryRanges {
2+
numSet: Set<number>;
3+
4+
constructor() {
5+
this.numSet = new Set();
6+
}
7+
8+
addNum(value: number): void {
9+
this.numSet.add(value);
10+
}
11+
12+
getIntervals(): number[][] {
13+
let nums = Array.from(this.numSet.keys());
14+
nums.sort((a, b) => a - b);
15+
let res: number[][] = [];
16+
17+
let i = 0;
18+
19+
while (i < nums.length) {
20+
const start = nums[i];
21+
22+
while (i + 1 < nums.length && nums[i] + 1 == nums[i + 1]) {
23+
i++;
24+
}
25+
26+
res.push([start, nums[i]]);
27+
i++;
28+
}
29+
30+
return res;
31+
}
32+
}

0 commit comments

Comments
 (0)