mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0056.合并区间.md
This commit is contained in:
@ -280,24 +280,22 @@ object Solution {
|
|||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn max(a: i32, b: i32) -> i32 {
|
pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||||
if a > b { a } else { b }
|
let mut res = vec![];
|
||||||
|
if intervals.is_empty() {
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
intervals.sort_by_key(|a| a[0]);
|
||||||
pub fn merge(intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
res.push(intervals[0].clone());
|
||||||
let mut intervals = intervals;
|
for interval in intervals.into_iter().skip(1) {
|
||||||
let mut result = Vec::new();
|
let res_last_ele = res.last_mut().unwrap();
|
||||||
if intervals.len() == 0 { return result; }
|
if res_last_ele[1] >= interval[0] {
|
||||||
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
|
res_last_ele[1] = interval[1].max(res_last_ele[1]);
|
||||||
result.push(intervals[0].clone());
|
|
||||||
for i in 1..intervals.len() {
|
|
||||||
if result.last_mut().unwrap()[1] >= intervals[i][0] {
|
|
||||||
result.last_mut().unwrap()[1] = Self::max(result.last_mut().unwrap()[1], intervals[i][1]);
|
|
||||||
} else {
|
} else {
|
||||||
result.push(intervals[i].clone());
|
res.push(interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user