题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
package main // import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ func isValid(s string) bool { // write code here slen := len(s) if slen%2 != 0 { return false } stack := make([]rune, 0) for _, c := range s { if c == '(' || c == '[' || c == '{' { stack = append(stack, c) } else if c == ')' || c == ']' || c == '}' { if len(stack) == 0 { return false } else if c == ')' && stack[len(stack)-1] == '(' { stack = stack[:len(stack)-1] } else if c == ']' && stack[len(stack)-1] == '[' { stack = stack[:len(stack)-1] } else if c == '}' && stack[len(stack)-1] == '{' { stack = stack[:len(stack)-1] } else { return false } } } if len(stack) == 0 { return true } else { return false } }