From 00dda7fc5c3d0f10578b762ab9374311cbe1889b Mon Sep 17 00:00:00 2001 From: erius Date: Mon, 8 Jul 2024 15:35:12 +0300 Subject: [PATCH] Implemented problems 26, 27, 28 and 29 TODO: improve problem 29 --- src/lib.rs | 4 ++ ...p26_remove_duplicates_from_sorted_array.rs | 37 +++++++++++ src/p27_remove_element.rs | 32 ++++++++++ ...ndex_of_the_first_occurence_in_a_string.rs | 21 +++++++ src/p29_divide_two_integers.rs | 63 +++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 src/p26_remove_duplicates_from_sorted_array.rs create mode 100644 src/p27_remove_element.rs create mode 100644 src/p28_find_the_index_of_the_first_occurence_in_a_string.rs create mode 100644 src/p29_divide_two_integers.rs diff --git a/src/lib.rs b/src/lib.rs index 2369b42..7bb4b6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,3 +25,7 @@ pub mod p22_generate_parentheses; pub mod p23_merge_k_sorted_lists; pub mod p24_swap_nodes_in_pairs; pub mod p25_reverse_nodes_in_k_group; +pub mod p26_remove_duplicates_from_sorted_array; +pub mod p27_remove_element; +pub mod p28_find_the_index_of_the_first_occurence_in_a_string; +pub mod p29_divide_two_integers; diff --git a/src/p26_remove_duplicates_from_sorted_array.rs b/src/p26_remove_duplicates_from_sorted_array.rs new file mode 100644 index 0000000..c39ddaa --- /dev/null +++ b/src/p26_remove_duplicates_from_sorted_array.rs @@ -0,0 +1,37 @@ +pub struct Solution; +impl Solution { + pub fn remove_duplicates(nums: &mut Vec) -> i32 { + let mut size = 1; + for i in 1..nums.len() { + if nums[i - 1] != nums[i] { + nums[size] = nums[i]; + size += 1; + } + } + nums.truncate(size); + return size as i32; + } +} + +#[cfg(test)] +mod test { + use super::Solution; + + #[test] + fn test1() { + let mut data = vec![1,1,2]; + let expected = vec![1,2]; + let result = Solution::remove_duplicates(&mut data); + assert_eq!(result, expected.len() as i32); + assert_eq!(data, expected); + } + + #[test] + fn test2() { + let mut data = vec![0,0,1,1,1,2,2,3,3,4]; + let expected = vec![0,1,2,3,4]; + let result = Solution::remove_duplicates(&mut data); + assert_eq!(result, expected.len() as i32); + assert_eq!(data, expected); + } +} diff --git a/src/p27_remove_element.rs b/src/p27_remove_element.rs new file mode 100644 index 0000000..3719848 --- /dev/null +++ b/src/p27_remove_element.rs @@ -0,0 +1,32 @@ +pub struct Solution; +impl Solution { + pub fn remove_element(nums: &mut Vec, val: i32) -> i32 { + nums.retain(|x| *x != val); + return nums.len() as i32; + } +} + +#[cfg(test)] +mod tests { + use super::Solution; + + #[test] + fn test1() { + let expected = vec![2,2]; + let mut data = vec![3,2,2,3]; + let result = Solution::remove_element(&mut data, 3); + data.sort(); + assert_eq!(data, expected); + assert_eq!(result, expected.len() as i32); + } + + #[test] + fn test2() { + let expected = vec![0,0,1,3,4]; + let mut data = vec![0,1,2,2,3,0,4,2]; + let result = Solution::remove_element(&mut data, 2); + data.sort(); + assert_eq!(data, expected); + assert_eq!(result, expected.len() as i32); + } +} diff --git a/src/p28_find_the_index_of_the_first_occurence_in_a_string.rs b/src/p28_find_the_index_of_the_first_occurence_in_a_string.rs new file mode 100644 index 0000000..6087458 --- /dev/null +++ b/src/p28_find_the_index_of_the_first_occurence_in_a_string.rs @@ -0,0 +1,21 @@ +pub struct Solution; +impl Solution { + pub fn str_str(haystack: String, needle: String) -> i32 { + return haystack.find(&needle).map(|x| x as i32).unwrap_or(-1); + } +} + +#[cfg(test)] +mod tests { + use super::Solution; + + #[test] + fn test1() { + assert_eq!(Solution::str_str("sadbutsad".to_string(), "sad".to_string()), 0); + } + + #[test] + fn test2() { + assert_eq!(Solution::str_str("leetcode".to_string(), "leeto".to_string()), -1); + } +} diff --git a/src/p29_divide_two_integers.rs b/src/p29_divide_two_integers.rs new file mode 100644 index 0000000..91d8a95 --- /dev/null +++ b/src/p29_divide_two_integers.rs @@ -0,0 +1,63 @@ +pub struct Solution; +impl Solution { + // terrible solutionE + // should use approximation using shifts + pub fn divide(mut dividend: i32, mut divisor: i32) -> i32 { + if dividend == i32::MAX && divisor == i32::MIN { + return 0; + } + if divisor == 1 { + return dividend; + } + if divisor == -1 { + if dividend == i32::MIN { + return i32::MAX; + } + return -dividend; + } + let mut result = 0; + let is_neg = (dividend < 0) ^ (divisor < 0); + let mut one = false; + dividend = if dividend == i32::MIN { + one = true; + i32::MAX + } else { dividend.abs() }; + divisor = if divisor == i32::MIN { i32::MAX } else { divisor.abs() }; + while dividend >= divisor { + dividend -= divisor; + result += 1; + } + if one && dividend - divisor == -1 { + result += 1; + } + if is_neg { + result = -result; + } + return result; + } +} + +#[cfg(test)] +mod tests { + use super::Solution; + + #[test] + fn test1() { + assert_eq!(Solution::divide(10, 3), 3); + } + + #[test] + fn test2() { + assert_eq!(Solution::divide(7, -3), -2); + } + + #[test] + fn test3() { + assert_eq!(Solution::divide(-2147483648, 2), -1073741824); + } + + #[test] + fn test4() { + assert_eq!(Solution::divide(-2147483648, -1), 2147483647); + } +}