Files
LeetCode-Go/leetcode/0020.Valid-Parentheses/20. Valid Parentheses.go
2020-08-07 17:06:53 +08:00

22 lines
541 B
Go

package leetcode
func isValid(s string) bool {
// 空字符串直接返回 true
if len(s) == 0 {
return true
}
stack := make([]rune, 0)
for _, v := range s {
if (v == '[') || (v == '(') || (v == '{') {
stack = append(stack, v)
} else if ((v == ']') && len(stack) > 0 && stack[len(stack)-1] == '[') ||
((v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(') ||
((v == '}') && len(stack) > 0 && stack[len(stack)-1] == '{') {
stack = stack[:len(stack)-1]
} else {
return false
}
}
return len(stack) == 0
}