From c3326eae14a66e42e6c007cdc8d2bed6f66d82c9 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 14:24:30 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index f2d78ade..ba50ad21 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -157,7 +157,26 @@ class Solution: ``` Go: +```Go +func isValid(s string) bool { + hash := map[byte]byte{')':'(', ']':'[', '}':'{'} + stack := make([]byte, 0) + if s == "" { + return true + } + for i := 0; i < len(s); i++ { + if s[i] == '(' || s[i] == '[' || s[i] == '{' { + stack = append(stack, s[i]) + } else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] { + stack = stack[:len(stack)-1] + } else { + return false + } + } + return len(stack) == 0 +} +```