add initial data
This commit is contained in:
1 parent
048a82b101
commit
51d935d06b
66 files changed
+1329
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,64 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
pub fn is_valid(s: String) -> bool {
|
||||
helper(s).unwrap_or(false)
|
||||
}
|
||||
|
||||
fn helper(s: String) -> Option<bool> {
|
||||
let mut stack = vec![];
|
||||
|
||||
for c in s.chars() {
|
||||
if ['(', '[', '{'].contains(&c) {
|
||||
stack.push(c);
|
||||
}
|
||||
if [')', ']', '}'].contains(&c) {
|
||||
if matches(c, *stack.last()?) {
|
||||
stack.pop();
|
||||
} else {
|
||||
return Some(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(stack.is_empty())
|
||||
}
|
||||
|
||||
fn matches(current: char, previous: char) -> bool {
|
||||
match current {
|
||||
')' => previous == '(',
|
||||
']' => previous == '[',
|
||||
'}' => previous == '{',
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::is_valid;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
assert!(is_valid("()".to_string()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
assert!(is_valid("()[]{}".to_string()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_3() {
|
||||
assert!(!is_valid("(]".to_string()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_4() {
|
||||
assert!(is_valid("([])".to_string()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_5() {
|
||||
assert!(!is_valid("([)]".to_string()))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user