Implemented p20, p21, removed redundant to_string() conversions in p17, p14, p12, p6 and p5

This commit is contained in:
Egor 2024-05-03 02:13:44 +03:00
parent 699239f5bd
commit 62e8eaf52f
9 changed files with 108 additions and 17 deletions

View file

@ -19,3 +19,6 @@ pub mod p16_3sum_closest;
pub mod p17_letter_combinations_of_a_phone_number;
pub mod p18_4sum;
pub mod p19_remove_nth_node_from_end_of_list;
pub mod p20_valid_parentheses;
pub mod p21_merge_two_sorted_lists;
pub mod p22_generate_parentheses;

View file

@ -35,15 +35,15 @@ impl Solution {
#[test]
fn test1() {
assert_eq!(Solution::int_to_roman(3), "III".to_string());
assert_eq!(Solution::int_to_roman(3), "III");
}
#[test]
fn test2() {
assert_eq!(Solution::int_to_roman(58), "LVIII".to_string());
assert_eq!(Solution::int_to_roman(58), "LVIII");
}
#[test]
fn test3() {
assert_eq!(Solution::int_to_roman(1994), "MCMXCIV".to_string());
assert_eq!(Solution::int_to_roman(1994), "MCMXCIV");
}

View file

@ -20,11 +20,11 @@ impl Solution {
#[test]
fn test1() {
let strs = vec!["flower".to_string(),"flow".to_string(),"flight".to_string()];
assert_eq!(Solution::longest_common_prefix(strs), "fl".to_string());
assert_eq!(Solution::longest_common_prefix(strs), "fl");
}
#[test]
fn test2() {
let strs = vec!["dog".to_string(),"racecar".to_string(),"car".to_string()];
assert_eq!(Solution::longest_common_prefix(strs), "".to_string());
assert_eq!(Solution::longest_common_prefix(strs), "");
}

View file

@ -27,11 +27,7 @@ impl Solution {
fn test1() {
let mut actual = Solution::letter_combinations("23".to_string());
actual.sort();
assert_eq!(actual, vec![
"ad".to_string(), "ae".to_string(), "af".to_string(),
"bd".to_string(), "be".to_string(), "bf".to_string(),
"cd".to_string(), "ce".to_string(), "cf".to_string()
]);
assert_eq!(actual, vec!["ad","ae","af","bd","be","bf","cd","ce","cf"]);
}
#[test]
@ -41,6 +37,5 @@ fn test2() {
#[test]
fn test3() {
assert_eq!(Solution::letter_combinations("2".to_string()),
vec!["a".to_string(), "b".to_string(), "c".to_string()]);
assert_eq!(Solution::letter_combinations("2".to_string()), vec!["a","b","c"]);
}

View file

@ -0,0 +1,36 @@
pub struct Solution;
impl Solution {
pub fn is_valid(s: String) -> bool {
let s = s.as_bytes();
let mut braces = vec![];
for i in 0..s.len() {
match s[i] {
b'(' => braces.push(b')'),
b'[' => braces.push(b']'),
b'{' => braces.push(b'}'),
b')' | b']' | b'}' => {
let Some(brace) = braces.pop()
else { return false };
if brace != s[i] { return false; }
},
_ => ()
}
}
return braces.is_empty();
}
}
#[test]
fn test1() {
assert_eq!(Solution::is_valid("()".to_string()), true);
}
#[test]
fn test2() {
assert_eq!(Solution::is_valid("()[]{}".to_string()), true);
}
#[test]
fn test3() {
assert_eq!(Solution::is_valid("(]".to_string()), false);
}

View file

@ -0,0 +1,43 @@
use super::linked_list::ListNode;
pub struct Solution;
impl Solution {
pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut head = None;
let mut tail = &mut head;
let (mut l1, mut l2) = (&list1, &list2);
while l1.is_some() || l2.is_some() {
let val1 = if l1.is_some() {
l1.as_ref().unwrap().val
} else { i32::MAX };
let val2 = if l2.is_some() {
l2.as_ref().unwrap().val
} else { i32::MAX };
let num = if val1 < val2 {
l1 = &l1.as_ref().unwrap().next;
val1
} else {
l2 = &l2.as_ref().unwrap().next;
val2
};
*tail = Some(Box::new(ListNode::new(num)));
tail = &mut tail.as_mut().unwrap().next;
}
return head;
}
}
#[test]
fn test1() {
assert_eq!(Solution::merge_two_lists(list![1,2,4], list![1,3,4]), list![1,1,2,3,4,4]);
}
#[test]
fn test2() {
assert_eq!(Solution::merge_two_lists(list![], list![]), list![]);
}
#[test]
fn test3() {
assert_eq!(Solution::merge_two_lists(list![], list![0]), list![0]);
}

View file

@ -0,0 +1,14 @@
pub struct Solution;
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
return vec![];
}
}
#[test]
fn test1() {
let mut actual = Solution::generate_parenthesis(3);
actual.sort();
assert_eq!(actual, vec!["((()))","(()())","(())()","()(())","()()()"]);
}

View file

@ -18,10 +18,10 @@ impl Solution {
#[test]
fn test1() {
let actual = Solution::longest_palindrome("babad".to_string());
assert!(actual == "bab".to_string() || actual == "aba".to_string());
assert!(actual == "bab" || actual == "aba");
}
#[test]
fn test2() {
assert_eq!(Solution::longest_palindrome("cbbd".to_string()), "bb".to_string());
assert_eq!(Solution::longest_palindrome("cbbd".to_string()), "bb");
}

View file

@ -20,15 +20,15 @@ impl Solution {
#[test]
fn test1() {
assert_eq!(Solution::convert("PAYPALISHIRING".to_string(), 3), "PAHNAPLSIIGYIR".to_string());
assert_eq!(Solution::convert("PAYPALISHIRING".to_string(), 3), "PAHNAPLSIIGYIR");
}
#[test]
fn test2() {
assert_eq!(Solution::convert("PAYPALISHIRING".to_string(), 4), "PINALSIGYAHRPI".to_string());
assert_eq!(Solution::convert("PAYPALISHIRING".to_string(), 4), "PINALSIGYAHRPI");
}
#[test]
fn test3() {
assert_eq!(Solution::convert("A".to_string(), 1), "A".to_string());
assert_eq!(Solution::convert("A".to_string(), 1), "A");
}