题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
package main
/**
*
* @param s string字符串
* @return bool布尔型
*/
func isValid(s string) bool {
sta := make([]int32, len(s))
top := 0
for _, ch := range s {
if ch == '{' || ch == '[' || ch == '(' {
sta[top] = ch
top++
} else {
if top <= 0 {
return false
} else if ch == '}' {
if sta[top-1] != '{' {
return false
}
} else if ch == ']' {
if sta[top-1] != '[' {
return false
}
} else if ch == ')' {
if sta[top-1] != '(' {
return false
}
}
top--
}
}
return top == 0
}
查看13道真题和解析
