20240515 Finished

This commit is contained in:
jackfiled 2024-05-15 14:23:43 +08:00
parent a503d481d4
commit 25ed407f17
2 changed files with 51 additions and 1 deletions

View File

@ -132,3 +132,4 @@ mod p219_contains_duplicate_ii;
mod p128_longest_consecutive_sequence; mod p128_longest_consecutive_sequence;
mod p228_summary_ranges; mod p228_summary_ranges;
mod p56_merge_intervals; mod p56_merge_intervals;
mod p57_insert_interval;

View File

@ -0,0 +1,49 @@
/**
* [57] Insert Interval
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn insert(intervals: Vec<Vec<i32>>, new_interval: Vec<i32>) -> Vec<Vec<i32>> {
let (mut left, mut right) = (new_interval[0], new_interval[1]);
let mut result = vec![];
let mut placed = false;
for interval in intervals {
if interval[1] < left {
result.push(interval);
} else if interval[0] > right {
if !placed {
result.push(vec![left, right]);
placed = true;
}
result.push(interval);
} else {
left = left.min(interval[0]);
right = right.max(interval[1]);
}
}
if !placed {
result.push(vec![left, right]);
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_57() {
}
}