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