diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 627ca5b1..26a8010d 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -329,5 +329,31 @@ object Solution { } ``` +### Rust + +```Rust +impl Solution { + fn max(a: i32, b: i32) -> i32 { + if a > b { a } else { b } + } + + pub fn merge(intervals: Vec>) -> Vec> { + let mut intervals = intervals; + let mut result = Vec::new(); + if intervals.len() == 0 { return result; } + intervals.sort_by(|a, b| a[0].cmp(&b[0])); + 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 { + result.push(intervals[i].clone()); + } + } + result + } +} +``` + -----------------------