From 2e5466bdce0a3d884a1a9084da74732a80fe806d Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:41:54 +0800 Subject: [PATCH] =?UTF-8?q?update=200020.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 3d986a82..69713c62 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -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 = 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() + } +} +``` +