From cd3c852e287435dc8a5b6188e7c6ed6025990116 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Thu, 4 Aug 2022 17:29:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0056.合并区间 Rust版本 --- problems/0056.合并区间.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 + } +} +``` + -----------------------