Implemented problem 14, improved readability of problems 3 and 4

This commit is contained in:
Egor 2024-04-19 01:46:23 +03:00
parent 67a8391ab8
commit c56399ab5d
4 changed files with 23 additions and 14 deletions

View file

@ -2,15 +2,18 @@ fn main() {
}
// TODO: imlpement
pub fn longest_common_prefix(strs: Vec<String>) -> 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;
}

8
src/bin/15_3sum.rs Normal file
View file

@ -0,0 +1,8 @@
fn main() {
}
pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
return vec![];
}

View file

@ -6,15 +6,15 @@ fn main() {
pub fn length_of_longest_substring(s: String) -> i32 {
let mut seen: HashMap<char, usize> = 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;
}

View file

@ -18,7 +18,5 @@ pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> 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;
}