20231212 Finished
This commit is contained in:
parent
87b26179ad
commit
27935d0bfa
|
@ -1 +1,3 @@
|
|||
mod s0001_two_sum;
|
||||
mod s0009_palindrome_number;
|
||||
mod s0020_valid_parentheses;
|
||||
|
|
74
src/solution/s0009_palindrome_number.rs
Normal file
74
src/solution/s0009_palindrome_number.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
/**
|
||||
* [9] Palindrome Number
|
||||
*
|
||||
* Given an integer x, return true if x is a <span data-keyword="palindrome-integer">palindrome</span>, and false otherwise.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: x = 121
|
||||
* Output: true
|
||||
* Explanation: 121 reads as 121 from left to right and from right to left.
|
||||
*
|
||||
* <strong class="example">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.
|
||||
*
|
||||
* <strong class="example">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));
|
||||
}
|
||||
}
|
88
src/solution/s0020_valid_parentheses.rs
Normal file
88
src/solution/s0020_valid_parentheses.rs
Normal file
|
@ -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:
|
||||
* <ol>
|
||||
* 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.
|
||||
* </ol>
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: s = "()"
|
||||
* Output: true
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: s = "()[]{}"
|
||||
* Output: true
|
||||
*
|
||||
* <strong class="example">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("(")));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user