From 479666d7a9b866448568373cc1ec9090a8c47f56 Mon Sep 17 00:00:00 2001 From: erius Date: Tue, 16 Apr 2024 16:09:58 +0300 Subject: [PATCH] Implemented problems 4-9 --- .vscode/settings.json | 3 ++- src/bin/1_two_sum.rs | 5 +++-- src/bin/2_add_two_numbers.rs | 1 + src/bin/4_median_of_two_sorted_arrays.rs | 20 ++++++++++++++++++-- src/bin/5_longest_palindrome_substring.rs | 21 +++++++++++++++++++++ src/bin/6_zigzag_conversion.rs | 20 ++++++++++++++++++++ src/bin/7_reverse_integer.rs | 19 +++++++++++++++++++ src/bin/8_string_to_integer.rs | 21 +++++++++++++++++++++ src/bin/9_palindrome_number.rs | 14 ++++++++++++++ 9 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 src/bin/5_longest_palindrome_substring.rs create mode 100644 src/bin/6_zigzag_conversion.rs create mode 100644 src/bin/7_reverse_integer.rs create mode 100644 src/bin/8_string_to_integer.rs create mode 100644 src/bin/9_palindrome_number.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 352a626..639564f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "rust-analyzer.linkedProjects": [ "./Cargo.toml" - ] + ], + "codetogether.virtualCursorJoin": "sharedVirtualCursor" } \ No newline at end of file diff --git a/src/bin/1_two_sum.rs b/src/bin/1_two_sum.rs index 17f9032..1eb1415 100644 --- a/src/bin/1_two_sum.rs +++ b/src/bin/1_two_sum.rs @@ -8,8 +8,9 @@ pub fn two_sum(nums: Vec, target: i32) -> Vec { let mut components: HashMap = HashMap::new(); for i in 0..nums.len() { let component = target - nums[i]; - let j = components.get(&component); - if j.is_some() { return vec![i as i32, *j.unwrap() as i32]; } + if let Some(j) = components.get(&component) { + return vec![i as i32, *j as i32]; + } components.insert(nums[i], i); } return Vec::new(); diff --git a/src/bin/2_add_two_numbers.rs b/src/bin/2_add_two_numbers.rs index 065bf43..c9585f0 100644 --- a/src/bin/2_add_two_numbers.rs +++ b/src/bin/2_add_two_numbers.rs @@ -2,6 +2,7 @@ fn main() { } +// TODO: implement algorithm pub fn add_two_numbers(l1: Option>, l2: Option>) -> Option> { return None; } diff --git a/src/bin/4_median_of_two_sorted_arrays.rs b/src/bin/4_median_of_two_sorted_arrays.rs index dc8a3fc..871783a 100644 --- a/src/bin/4_median_of_two_sorted_arrays.rs +++ b/src/bin/4_median_of_two_sorted_arrays.rs @@ -2,7 +2,23 @@ fn main() { } +// TODO: reduce algorithm complexity from O(m+n) to O(log(m+n)) pub fn find_median_sorted_arrays(nums1: Vec, nums2: Vec) -> f64 { - - return 0.0; + let length = nums1.len() + nums2.len(); + let mut nums3 = vec![0i32; length]; + let (mut m, mut n) = (0usize, 0usize); + while m + n < length { + let x1 = if m < nums1.len() { nums1[m] } else { i32::MAX }; + let x2 = if n < nums2.len() { nums2[n] } else { i32::MAX }; + if x1 <= x2 { + nums3[m + n] = x1; + m += 1; + } else { + nums3[m + n] = x2; + 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; } diff --git a/src/bin/5_longest_palindrome_substring.rs b/src/bin/5_longest_palindrome_substring.rs new file mode 100644 index 0000000..3c5899e --- /dev/null +++ b/src/bin/5_longest_palindrome_substring.rs @@ -0,0 +1,21 @@ +fn main() { + println!("{}", longest_palindrome(String::from("bccd"))); +} + +pub fn longest_palindrome(s: String) -> String { + if s.len() <= 1 { return s; } + let mut longest_palindrome = String::from(&s[0..1]); + for i in 0..s.len()-1 { + for even_palindrome_offset in 0..=1 { + let (mut start, mut end) = (i + even_palindrome_offset, i); + while start > 0 && end < s.len() - 1 { + if s.as_bytes()[start - 1] != s.as_bytes()[end + 1] { break; } + start -= 1; end += 1; + } + if end + 1 - start > longest_palindrome.len() { + longest_palindrome = String::from(&s[start..end+1]); + } + } + } + return longest_palindrome; +} diff --git a/src/bin/6_zigzag_conversion.rs b/src/bin/6_zigzag_conversion.rs new file mode 100644 index 0000000..414175b --- /dev/null +++ b/src/bin/6_zigzag_conversion.rs @@ -0,0 +1,20 @@ +fn main() { + println!("{}", convert(String::from("ABC"), 4)) +} + +pub fn convert(s: String, num_rows: i32) -> String { + if num_rows <= 1 { return s; } + let mut result = String::with_capacity(s.len()); + let num_rows = num_rows as usize; + for i in 0..num_rows { + for j in (i..s.len()).step_by(num_rows * 2 - 2) { + result.push(s.as_bytes()[j] as char); + if i > 0 && i < num_rows - 1 { + let diagonal_index = j + 2 * (num_rows - i - 1); + if diagonal_index >= s.len() { continue; } + result.push(s.as_bytes()[diagonal_index] as char); + } + } + } + return result; +} diff --git a/src/bin/7_reverse_integer.rs b/src/bin/7_reverse_integer.rs new file mode 100644 index 0000000..4a9713f --- /dev/null +++ b/src/bin/7_reverse_integer.rs @@ -0,0 +1,19 @@ +fn main() { + println!("{}", reverse(1534236469)); +} + +// problem doesn't allow using 64-bit numbers +pub fn reverse(x: i32) -> i32 { + let is_negative = x < 0; + let (mut x, mut result) = (x.checked_abs().unwrap_or(0), 0i32); + while x > 9 { + result *= 10; + result += x % 10; + x /= 10; + } + return result.checked_mul(10).and_then(|mut n| { + n += x % 10; + if is_negative { n = -n; } + Some(n) + }).unwrap_or(0); +} diff --git a/src/bin/8_string_to_integer.rs b/src/bin/8_string_to_integer.rs new file mode 100644 index 0000000..673a183 --- /dev/null +++ b/src/bin/8_string_to_integer.rs @@ -0,0 +1,21 @@ +fn main() { + println!("{}", my_atoi(String::from("2147483648"))); +} + +pub fn my_atoi(s: String) -> i32 { + let s = s.trim(); + let is_neg = s.starts_with('-'); + let skip_sign = if is_neg || s.starts_with('+') { 1 } else { 0 }; + let mut result = 0i32; + for byte in s.bytes().skip(skip_sign) { + if byte < b'0' || byte > b'9' { break; } + let digit = (byte - b'0') as i32; + if let Some(checked_result) = result.checked_mul(10).and_then(|n| n.checked_add(digit)) { + result = checked_result; + } else { + return if is_neg { i32::MIN } else { i32::MAX }; + } + } + if is_neg { result = -result; } + return result; +} diff --git a/src/bin/9_palindrome_number.rs b/src/bin/9_palindrome_number.rs new file mode 100644 index 0000000..a5b717e --- /dev/null +++ b/src/bin/9_palindrome_number.rs @@ -0,0 +1,14 @@ +fn main() { + +} + +pub fn is_palindrome(x: i32) -> bool { + if x < 0 { return false; } + let (mut x_copy, mut reversed) = (x as u32, 0u32); + while x_copy > 0 { + reversed *= 10; + reversed += x_copy % 10; + x_copy /= 10; + } + return reversed == x as u32; +}