1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| // @Title: 有效的括号 (Valid Parentheses)
// @Author: 15816537946@163.com
// @Date: 2020-11-01 09:53:47
// @Runtime: 0 ms
// @Memory: 1.9 MB
func isValid(s string) bool {
stack := make([]byte, len(s))
var top int
for _, c := range s {
switch c {
case '(':
stack[top] = ')'
top++
case '{':
stack[top] = '}'
top++
case '[':
stack[top] = ']'
top++
case '}', ']', ')':
if top > 0 && stack[top-1] == byte(c) {
top--
} else {
return false
}
}
}
return top == 0
}
|