Improved problem 5 runtime, removed unneeded TODO from problem 15

This commit is contained in:
Egor 2024-04-23 03:14:31 +03:00
parent c9e582b553
commit 955182542e
3 changed files with 18 additions and 15 deletions

View file

@ -4,7 +4,6 @@ fn main() {
println!("{:?}", three_sum(vec![-2,0,0,2,2]));
}
// TODO: fix and optimize
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result = Vec::<Vec<i32>>::new();
nums.sort();

View file

@ -0,0 +1,8 @@
fn main() {
}
pub fn three_sum_closest(nums: Vec<i32>, target: i32) -> i32 {
return 0;
}

View file

@ -1,21 +1,17 @@
fn main() {
println!("{}", longest_palindrome(String::from("bccd")));
println!("{}", longest_palindrome(String::from("bb")));
}
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]);
}
let mut max_indexes = (0usize, 1usize);
for i in 0..(s.len()-1)*2 {
let (mut start, mut end) = (i / 2 + i % 2, i / 2);
while start > 0 && end < s.len() - 1 {
if s.as_bytes()[start - 1] != s.as_bytes()[end + 1] { break; }
start -= 1; end += 1;
}
let length = end + 1 - start;
if length > max_indexes.1 - max_indexes.0 { max_indexes = (start, end + 1); }
}
return longest_palindrome;
return String::from(&s[max_indexes.0..max_indexes.1]);
}