diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 1c261c7..18fdf00 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -1 +1,3 @@ mod s0001_two_sum; +mod s0009_palindrome_number; +mod s0020_valid_parentheses; diff --git a/src/solution/s0009_palindrome_number.rs b/src/solution/s0009_palindrome_number.rs new file mode 100644 index 0000000..c6b55ad --- /dev/null +++ b/src/solution/s0009_palindrome_number.rs @@ -0,0 +1,74 @@ +/** + * [9] Palindrome Number + * + * Given an integer x, return true if x is a palindrome, and false otherwise. + * + * Example 1: + * + * Input: x = 121 + * Output: true + * Explanation: 121 reads as 121 from left to right and from right to left. + * + * Example 2: + * + * Input: x = -121 + * Output: false + * Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. + * + * Example 3: + * + * Input: x = 10 + * Output: false + * Explanation: Reads 01 from right to left. Therefore it is not a palindrome. + * + * + * Constraints: + * + * -2^31 <= x <= 2^31 - 1 + * + * + * Follow up: Could you solve it without converting the integer to a string? + */ +pub struct Solution {} + +// problem: https://leetcode.cn/problems/palindrome-number/ +// discuss: https://leetcode.cn/problems/palindrome-number/discuss/?currentPage=1&orderBy=most_votes&query= + +// submission codes start here + +impl Solution { + pub fn is_palindrome(x: i32) -> bool { + if x == 0 { + return true + } + + if x < 0 || x % 10 == 0 { + return false; + } + + let mut reverse_half = x % 10; + let mut x = x / 10; + + while x > reverse_half { + reverse_half = reverse_half * 10 + x % 10; + x = x / 10; + } + + x == reverse_half || x == reverse_half / 10 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_9() { + assert_eq!(true, Solution::is_palindrome(121)); + assert_eq!(false, Solution::is_palindrome(123)); + assert_eq!(false, Solution::is_palindrome(10)); + assert_eq!(true, Solution::is_palindrome(0)); + } +} diff --git a/src/solution/s0020_valid_parentheses.rs b/src/solution/s0020_valid_parentheses.rs new file mode 100644 index 0000000..d1bef4a --- /dev/null +++ b/src/solution/s0020_valid_parentheses.rs @@ -0,0 +1,88 @@ +/** + * [20] Valid Parentheses + * + * Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. + * An input string is valid if: + *
    + * Open brackets must be closed by the same type of brackets. + * Open brackets must be closed in the correct order. + * Every close bracket has a corresponding open bracket of the same type. + *
+ * + * Example 1: + * + * Input: s = "()" + * Output: true + * + * Example 2: + * + * Input: s = "()[]{}" + * Output: true + * + * Example 3: + * + * Input: s = "(]" + * Output: false + * + * + * Constraints: + * + * 1 <= s.length <= 10^4 + * s consists of parentheses only '()[]{}'. + * + */ +pub struct Solution {} + +// problem: https://leetcode.cn/problems/valid-parentheses/ +// discuss: https://leetcode.cn/problems/valid-parentheses/discuss/?currentPage=1&orderBy=most_votes&query= + +// submission codes start here + +impl Solution { + pub fn is_valid(s: String) -> bool { + let left = vec!['(', '[', '{']; + let right = vec![')', ']', '}']; + + let mut stack = Vec::with_capacity(s.len()); + + for c in s.chars() { + if left.contains(&c) { + stack.push(c) + } + else if right.contains(&c) { + let target = match c { + ')' => '(', + ']' => '[', + '}' => '{', + _ => return false + }; + + if stack.ends_with(&[target]) { + stack.pop(); + } + else { + return false + } + } else { + return false + } + } + + stack.is_empty() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_20() { + assert_eq!(true, Solution::is_valid(String::from("()"))); + assert_eq!(true, Solution::is_valid(String::from("()[]{}"))); + assert_eq!(false, Solution::is_valid(String::from("(]"))); + assert_eq!(false, Solution::is_valid(String::from("("))); + } +}