-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-intervals.rs
51 lines (45 loc) · 1.43 KB
/
merge-intervals.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
49
50
51
#![allow(unused)]
/// https://leetcode-cn.com/problems/merge-intervals/
struct Solution;
impl Solution {
pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
intervals.sort_by(|x, y| x[0].cmp(&y[0]));
let mut v: Vec<Vec<i32>> = Vec::new();
for i in intervals.into_iter() {
if v.is_empty() || v.last().unwrap()[1] < i[0] {
// 如果当前v为空,则直接插入进去
// 如果v中最后一个元素的end值,小于i的start元素,说明i此时还在v的最后一个元素的范围中。
v.push(i);
} else {
// 获取结束范围值最大的数,作为end的值
let end = v.last_mut().unwrap();
end[1] = i32::max(end[1], i[1]);
}
}
v
}
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn merge() {
assert_eq!(
Solution::merge(vec![vec![1, 3], vec![2, 6], vec![8, 10], vec![15, 18]]),
vec![vec![1, 6], vec![8, 10], vec![15, 18]]
);
assert_eq!(
Solution::merge(vec![vec![1, 4], vec![4, 5]]),
vec![vec![1, 5]]
);
assert_eq!(
Solution::merge(vec![vec![1, 4], vec![0, 4]]),
vec![vec![0, 4]]
);
assert_eq!(
Solution::merge(vec![vec![1, 4], vec![0, 1]]),
vec![vec![0, 4]]
);
}
}
fn main() {}