Skip to content

Commit 57e687c

Browse files
committed
1
1 parent 0202122 commit 57e687c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

notes/src/day36/lc56.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,27 @@ public:
2626
2727
这个 很直接
2828
29+
30+
```rust
31+
struct Solution {}
32+
impl Solution {
33+
pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
34+
let mut res: Vec<Vec<i32>> = Vec::new();
35+
intervals.sort_by_key(|v| v[0]);
36+
let mut left: i32 = intervals[0][0];
37+
let mut right: i32 = intervals[0][1];
38+
intervals.iter().skip(1usize).for_each(|v| {
39+
if v[0] <= right {
40+
right = std::cmp::max(v[1], right);
41+
} else {
42+
// non overlap
43+
res.push(vec![left, right]);
44+
left = v[0];
45+
right = v[1];
46+
}
47+
});
48+
res.push(vec![left, right]);
49+
res
50+
}
51+
}
52+
```

0 commit comments

Comments
 (0)