Improved problem 5 runtime, removed unneeded TODO from problem 15
This commit is contained in:
parent
c9e582b553
commit
955182542e
3 changed files with 18 additions and 15 deletions
|
@ -4,7 +4,6 @@ fn main() {
|
||||||
println!("{:?}", three_sum(vec![-2,0,0,2,2]));
|
println!("{:?}", three_sum(vec![-2,0,0,2,2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: fix and optimize
|
|
||||||
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||||
let mut result = Vec::<Vec<i32>>::new();
|
let mut result = Vec::<Vec<i32>>::new();
|
||||||
nums.sort();
|
nums.sort();
|
||||||
|
|
8
src/bin/16_3sum_closest.rs
Normal file
8
src/bin/16_3sum_closest.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn three_sum_closest(nums: Vec<i32>, target: i32) -> i32 {
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -1,21 +1,17 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{}", longest_palindrome(String::from("bccd")));
|
println!("{}", longest_palindrome(String::from("bb")));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn longest_palindrome(s: String) -> String {
|
pub fn longest_palindrome(s: String) -> String {
|
||||||
if s.len() <= 1 { return s; }
|
let mut max_indexes = (0usize, 1usize);
|
||||||
let mut longest_palindrome = String::from(&s[0..1]);
|
for i in 0..(s.len()-1)*2 {
|
||||||
for i in 0..s.len()-1 {
|
let (mut start, mut end) = (i / 2 + i % 2, i / 2);
|
||||||
for even_palindrome_offset in 0..=1 {
|
while start > 0 && end < s.len() - 1 {
|
||||||
let (mut start, mut end) = (i + even_palindrome_offset, i);
|
if s.as_bytes()[start - 1] != s.as_bytes()[end + 1] { break; }
|
||||||
while start > 0 && end < s.len() - 1 {
|
start -= 1; end += 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 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]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue