-
Notifications
You must be signed in to change notification settings - Fork 1
/
57.insert-interval.rs
49 lines (39 loc) · 1.1 KB
/
57.insert-interval.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @lc app=leetcode id=57 lang=rust
*
* [57] Insert Interval
*/
// @lc code=start
use std::mem;
use std::cmp;
impl Solution {
pub fn insert(mut intervals: Vec<Vec<i32>>, new_interval: Vec<i32>) -> Vec<Vec<i32>> {
let N = intervals.len();
if N == 0 { return vec![new_interval]; }
let mut ans = vec![];
let mut i = 0;
while i < N && intervals[i][1] < new_interval[0] {
ans.push(mem::replace(&mut intervals[i], vec![]));
i += 1;
}
if i < N {
let left = cmp::min(intervals[i][0], new_interval[0]);
let mut right = new_interval[1];
while i < N && intervals[i][0] <= new_interval[1] {
if intervals[i][1] > right {
right = intervals[i][1];
}
i += 1;
}
ans.push(vec![left, right]);
} else {
ans.push(new_interval);
}
while i < N {
ans.push(mem::replace(&mut intervals[i], vec![]));
i += 1;
}
ans
}
}
// @lc code=end