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