From b91240aeeffa7037448bfd4e65c6a62a1425cdb4 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 13 May 2024 13:15:46 +0800 Subject: [PATCH 1/3] 20240513 Finished --- src/problem/mod.rs | 3 +- src/problem/p202_happy_number.rs | 3 +- src/problem/p228_summary_ranges.rs | 54 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/problem/p228_summary_ranges.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 164079f..30785e6 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -129,4 +129,5 @@ mod p242_valid_anagram; mod p49_group_anagrams; mod p202_happy_number; mod p219_contains_duplicate_ii; -mod p128_longest_consecutive_sequence; \ No newline at end of file +mod p128_longest_consecutive_sequence; +mod p228_summary_ranges; \ No newline at end of file diff --git a/src/problem/p202_happy_number.rs b/src/problem/p202_happy_number.rs index e2b33e0..a5d28aa 100644 --- a/src/problem/p202_happy_number.rs +++ b/src/problem/p202_happy_number.rs @@ -4,7 +4,7 @@ pub struct Solution {} // submission codes start here -use std::{collections::HashSet, ops::Add}; +use std::collections::HashSet; impl Solution { pub fn is_happy(n: i32) -> bool { @@ -13,7 +13,6 @@ impl Solution { let mut n = Solution::calculate_square(n); while n != 1 { - dbg!(n); if s.contains(&n) { return false; } diff --git a/src/problem/p228_summary_ranges.rs b/src/problem/p228_summary_ranges.rs new file mode 100644 index 0000000..b931932 --- /dev/null +++ b/src/problem/p228_summary_ranges.rs @@ -0,0 +1,54 @@ +/** + * [228] Summary Ranges + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn summary_ranges(nums: Vec) -> Vec { + let mut result = vec![]; + + if nums.len() == 0 { + return result; + } + + let mut begin = nums[0]; + let mut end = nums[0]; + + for i in 1..nums.len() { + if nums[i] == end + 1 { + end += 1; + } else { + result.push(if begin == end { + format!("{}", begin) + } else { + format!("{}->{}", begin, end) + }); + + begin = nums[i]; + end = nums[i]; + } + } + + result.push(if begin == end { + format!("{}", begin) + } else { + format!("{}->{}", begin, end) + }); + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_228() { + } +} From a503d481d49694e1715df134e38f45c18722fe40 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 14 May 2024 18:14:00 +0800 Subject: [PATCH 2/3] 20240514 Finished --- src/problem/mod.rs | 3 +- src/problem/p56_merge_intervals.rs | 47 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/problem/p56_merge_intervals.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 30785e6..047020a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -130,4 +130,5 @@ mod p49_group_anagrams; mod p202_happy_number; mod p219_contains_duplicate_ii; mod p128_longest_consecutive_sequence; -mod p228_summary_ranges; \ No newline at end of file +mod p228_summary_ranges; +mod p56_merge_intervals; \ No newline at end of file diff --git a/src/problem/p56_merge_intervals.rs b/src/problem/p56_merge_intervals.rs new file mode 100644 index 0000000..026be90 --- /dev/null +++ b/src/problem/p56_merge_intervals.rs @@ -0,0 +1,47 @@ +/** + * [56] Merge Intervals + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn merge(intervals: Vec>) -> Vec> { + let mut intervals: Vec<(i32, i32)> = intervals.iter().map(|p| (p[0], p[1])).collect(); + + intervals.sort_unstable(); + + let mut result: Vec<(i32, i32)> = vec![intervals[0]]; + let mut last = 0; + + for i in 1..intervals.len() { + let (begin, end) = intervals[i]; + let (last_begin, last_end) = result[last]; + + if begin <= last_end { + result[last].0 = result[last].0.min(begin); + result[last].1 = result[last].1.max(end); + } else { + result.push(intervals[i]); + last += 1; + } + } + + result.iter().map(|p| vec![p.0, p.1]).collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_56() { + assert_eq!( + vec![vec![1, 6], vec![8, 10], vec![15, 18]], + Solution::merge(vec![vec![1, 3], vec![2, 6], vec![8, 10], vec![15, 18]]) + ) + } +} From 25ed407f1744f34f5fc0a21a86c42dc567c83e54 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 15 May 2024 14:23:43 +0800 Subject: [PATCH 3/3] 20240515 Finished --- src/problem/mod.rs | 3 +- src/problem/p57_insert_interval.rs | 49 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/problem/p57_insert_interval.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 047020a..92bd317 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -131,4 +131,5 @@ mod p202_happy_number; mod p219_contains_duplicate_ii; mod p128_longest_consecutive_sequence; mod p228_summary_ranges; -mod p56_merge_intervals; \ No newline at end of file +mod p56_merge_intervals; +mod p57_insert_interval; \ No newline at end of file diff --git a/src/problem/p57_insert_interval.rs b/src/problem/p57_insert_interval.rs new file mode 100644 index 0000000..859ad40 --- /dev/null +++ b/src/problem/p57_insert_interval.rs @@ -0,0 +1,49 @@ +/** + * [57] Insert Interval + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn insert(intervals: Vec>, new_interval: Vec) -> Vec> { + 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() { + } +}