mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user