diff --git a/src/bin/15_3sum.rs b/src/bin/15_3sum.rs index 5494883..27f0fd2 100644 --- a/src/bin/15_3sum.rs +++ b/src/bin/15_3sum.rs @@ -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) -> Vec> { let mut result = Vec::>::new(); nums.sort(); diff --git a/src/bin/16_3sum_closest.rs b/src/bin/16_3sum_closest.rs new file mode 100644 index 0000000..ac71fee --- /dev/null +++ b/src/bin/16_3sum_closest.rs @@ -0,0 +1,8 @@ +fn main() { + +} + +pub fn three_sum_closest(nums: Vec, target: i32) -> i32 { + + return 0; +} diff --git a/src/bin/5_longest_palindrome_substring.rs b/src/bin/5_longest_palindrome_substring.rs index 3c5899e..07897de 100644 --- a/src/bin/5_longest_palindrome_substring.rs +++ b/src/bin/5_longest_palindrome_substring.rs @@ -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]); }