func isValid(s string) bool {
if s == "" {
return true
}
if len(s)%2 == 1 {
return false
}
keyMap := map[string]string{
"}": "{",
"]": "[",
")": "(",
}
var table []string
for i := 0; i < len(s); i++ {
if len(table) > 0 {
tmp, ok := keyMap[string(s[i])]
if ok {
top := table[len(table)-1]
if top == tmp {
table = table[:len(table)-1]
continue
}
}
}
table = append(table, string(s[i]))
}
return len(table) == 0
}
力扣算法15-有效的括号
最新推荐文章于 2024-11-13 13:56:35 发布