package top100liked import ( "fmt" "testing" ) // https://leetcode.cn/problems/valid-parentheses/description/?envType=study-plan-v2&envId=top-100-liked func isValid(s string) bool { stack := []rune{} for _, c := range s { if c == '(' || c == '[' || c == '{' { stack = append(stack, c) } else { if len(stack) == 0 { return false } pre := stack[len(stack)-1] stack = stack[:len(stack)-1] if c == ')' && pre != '(' { return false } if c == ']' && pre != '[' { return false } if c == '}' && pre != '{' { return false } } } return len(stack) == 0 } func Test(t *testing.T) { fmt.Println(isValid("()")) fmt.Println(isValid("()[]{}")) fmt.Println(isValid("(]")) fmt.Println(isValid("([])")) fmt.Println(isValid("([)]")) fmt.Println(isValid("(")) fmt.Println(isValid("]")) }