Files
leetcode/top-100-liked/68/leetcode_test.go
T
2026-07-16 14:03:46 +08:00

44 lines
839 B
Go

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("]"))
}