mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0020.有效的括号.md
This commit is contained in:
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user