mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
update 0020.有效的括号.md about rust
This commit is contained in:
@ -493,6 +493,33 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn is_valid(s: String) -> bool {
|
||||
if s.len() % 2 == 1 {
|
||||
return false;
|
||||
}
|
||||
let mut stack = vec![];
|
||||
let mut chars: Vec<char> = s.chars().collect();
|
||||
while let Some(s) = chars.pop() {
|
||||
match s {
|
||||
')' => stack.push('('),
|
||||
']' => stack.push('['),
|
||||
'}' => stack.push('{'),
|
||||
_ => {
|
||||
if stack.is_empty() || stack.pop().unwrap() != s {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stack.is_empty()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user