From c56399ab5dfe553bbb00657cb74ab4d4e500ddcd Mon Sep 17 00:00:00 2001 From: erius Date: Fri, 19 Apr 2024 01:46:23 +0300 Subject: [PATCH] Implemented problem 14, improved readability of problems 3 and 4 --- src/bin/14_longest_common_prefix.rs | 17 ++++++++++------- src/bin/15_3sum.rs | 8 ++++++++ ...st_substring_without_repeating_characters.rs | 8 ++++---- src/bin/4_median_of_two_sorted_arrays.rs | 4 +--- 4 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 src/bin/15_3sum.rs diff --git a/src/bin/14_longest_common_prefix.rs b/src/bin/14_longest_common_prefix.rs index 5ca3583..c1f1d99 100644 --- a/src/bin/14_longest_common_prefix.rs +++ b/src/bin/14_longest_common_prefix.rs @@ -2,15 +2,18 @@ fn main() { } -// TODO: imlpement pub fn longest_common_prefix(strs: Vec) -> String { let mut result = String::with_capacity(200); - let index = 0; - loop { - let c = strs[0].as_bytes()[index]; - for str in strs.iter() { - + let first_str = strs[0].as_bytes(); + if first_str.is_empty() { return result; } + for i in 0..first_str.len() { + let c = first_str[i]; + for j in 1..strs.len() { + if i >= strs[j].len() || c != strs[j].as_bytes()[i] { + return result; + } } + result.push(c as char); } return result; -} \ No newline at end of file +} diff --git a/src/bin/15_3sum.rs b/src/bin/15_3sum.rs new file mode 100644 index 0000000..794535d --- /dev/null +++ b/src/bin/15_3sum.rs @@ -0,0 +1,8 @@ +fn main() { + +} + +pub fn three_sum(nums: Vec) -> Vec> { + + return vec![]; +} diff --git a/src/bin/3_longest_substring_without_repeating_characters.rs b/src/bin/3_longest_substring_without_repeating_characters.rs index 8a83af0..6abc5f5 100644 --- a/src/bin/3_longest_substring_without_repeating_characters.rs +++ b/src/bin/3_longest_substring_without_repeating_characters.rs @@ -6,15 +6,15 @@ fn main() { pub fn length_of_longest_substring(s: String) -> i32 { let mut seen: HashMap = HashMap::with_capacity(80); - let (mut start_index, mut max_length) = (0usize, 0i32); + let (mut start_index, mut max_length) = (0usize, 0usize); for (i, c) in s.chars().enumerate() { if let Some(index) = seen.get(&c) { - max_length = max_length.max((i - start_index) as i32); + max_length = max_length.max(i - start_index); start_index = *index + 1; seen.retain(|_, i| *i >= start_index); } seen.insert(c, i); } - max_length = max_length.max((s.len() - start_index) as i32); - return max_length; + max_length = max_length.max(s.len() - start_index); + return max_length as i32; } diff --git a/src/bin/4_median_of_two_sorted_arrays.rs b/src/bin/4_median_of_two_sorted_arrays.rs index 871783a..c7b1f09 100644 --- a/src/bin/4_median_of_two_sorted_arrays.rs +++ b/src/bin/4_median_of_two_sorted_arrays.rs @@ -18,7 +18,5 @@ pub fn find_median_sorted_arrays(nums1: Vec, nums2: Vec) -> f64 { n += 1; } } - let mut result = nums3[length / 2] as f64; - if length % 2 == 0 { result = (result + nums3[length / 2 - 1] as f64) / 2.0; } - return result; + return (nums3[(length - 1) / 2] + nums3[length / 2]) as f64 / 2.0; }