Implemented problems 4-9

This commit is contained in:
Egor 2024-04-16 16:09:58 +03:00
parent 5da5a2ca0a
commit 479666d7a9
9 changed files with 119 additions and 5 deletions

View file

@ -1,5 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
],
"codetogether.virtualCursorJoin": "sharedVirtualCursor"
}

View file

@ -8,8 +8,9 @@ pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut components: HashMap<i32, usize> = 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();

View file

@ -2,6 +2,7 @@ fn main() {
}
// TODO: implement algorithm
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
return None;
}

View file

@ -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<i32>, nums2: Vec<i32>) -> 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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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;
}