Update 0020.有效的括号.md

This commit is contained in:
QuinnDK
2021-05-13 14:24:30 +08:00
committed by GitHub
parent bff058e3c0
commit 8b30d5d1a7

View File

@ -157,7 +157,26 @@ class Solution:
``` ```
Go 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
}
```