leetcode 20 rust
題目

編程語(yǔ)言
rust
注意點(diǎn)
字符也可以用==,!=
代碼
pub fn is_valid(s: String) -> bool {
let mut p = Vec::new();
for i in s.chars() {
match i {
'[' => p.push(']'),
'(' => p.push(')'),
'{' => p.push('}'),
_ => {
if p.is_empty() || i != p.pop().unwrap() {
return false;
}
}
}
}
p.is_empty()
}
測(cè)試
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn test_1(){
assert_eq!(is_valid("()".to_string()),true);
}
#[test]
fn test_2(){
assert_eq!(is_valid("()[]{}".to_string()),true);
}
#[test]
fn test_3(){
assert_eq!(is_valid("(]".to_string()),false);
}
#[test]
fn test_4(){
assert_eq!(is_valid("{[]}".to_string()),true);
}
}
posted on 2021-04-08 13:34 GeniusOfCX 閱讀(43) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)