Skip to content

Commit 7af88a1

Browse files
author
zim0369
committed
Create 57-Insert-Interval.rs
1 parent 939af48 commit 7af88a1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

rust/57-Insert-Interval.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub fn insert(intervals: Vec<Vec<i32>>, mut new_interval: Vec<i32>) -> Vec<Vec<i32>> {
2+
let mut res: Vec<Vec<i32>> = Vec::new();
3+
4+
for i in 0..intervals.len() {
5+
if new_interval[1] < intervals[i][0] {
6+
res.push(new_interval.clone());
7+
return [res, intervals[i..].to_vec()].concat().to_vec();
8+
} else if new_interval[0] > intervals[i][1] {
9+
res.push(intervals[i].clone());
10+
} else {
11+
new_interval = vec![
12+
new_interval[0].min(intervals[i][0]),
13+
new_interval[1].max(intervals[i][1]),
14+
];
15+
}
16+
}
17+
18+
res.push(new_interval);
19+
20+
res
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy