diff --git a/src/lib.rs b/src/lib.rs index 7f7b725..00b3c4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/p12_integer_to_roman.rs b/src/p12_integer_to_roman.rs index 2ef0a4f..7db55c3 100644 --- a/src/p12_integer_to_roman.rs +++ b/src/p12_integer_to_roman.rs @@ -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"); } diff --git a/src/p14_longest_common_prefix.rs b/src/p14_longest_common_prefix.rs index e2f3147..4346b90 100644 --- a/src/p14_longest_common_prefix.rs +++ b/src/p14_longest_common_prefix.rs @@ -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), ""); } diff --git a/src/p17_letter_combinations_of_a_phone_number.rs b/src/p17_letter_combinations_of_a_phone_number.rs index 0caf26b..6e99723 100644 --- a/src/p17_letter_combinations_of_a_phone_number.rs +++ b/src/p17_letter_combinations_of_a_phone_number.rs @@ -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"]); } diff --git a/src/p20_valid_parentheses.rs b/src/p20_valid_parentheses.rs new file mode 100644 index 0000000..228265c --- /dev/null +++ b/src/p20_valid_parentheses.rs @@ -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); +} diff --git a/src/p21_merge_two_sorted_lists.rs b/src/p21_merge_two_sorted_lists.rs new file mode 100644 index 0000000..55cd7cf --- /dev/null +++ b/src/p21_merge_two_sorted_lists.rs @@ -0,0 +1,43 @@ +use super::linked_list::ListNode; + +pub struct Solution; +impl Solution { + pub fn merge_two_lists(list1: Option>, list2: Option>) -> Option> { + 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]); +} diff --git a/src/p22_generate_parentheses.rs b/src/p22_generate_parentheses.rs new file mode 100644 index 0000000..1f44d60 --- /dev/null +++ b/src/p22_generate_parentheses.rs @@ -0,0 +1,14 @@ +pub struct Solution; +impl Solution { + pub fn generate_parenthesis(n: i32) -> Vec { + + return vec![]; + } +} + +#[test] +fn test1() { + let mut actual = Solution::generate_parenthesis(3); + actual.sort(); + assert_eq!(actual, vec!["((()))","(()())","(())()","()(())","()()()"]); +} diff --git a/src/p5_longest_palindrome_substring.rs b/src/p5_longest_palindrome_substring.rs index 04a7342..e170242 100644 --- a/src/p5_longest_palindrome_substring.rs +++ b/src/p5_longest_palindrome_substring.rs @@ -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"); } diff --git a/src/p6_zigzag_conversion.rs b/src/p6_zigzag_conversion.rs index 1946c6e..f45bbbf 100644 --- a/src/p6_zigzag_conversion.rs +++ b/src/p6_zigzag_conversion.rs @@ -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"); }