Files
leetcode/top-100-liked/90/leetcode_test.go
T
2026-07-23 16:56:50 +08:00

51 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/longest-valid-parentheses/?envType=study-plan-v2&envId=top-100-liked
// 32. 最长有效括号
//
// 给你一个只包含 `'('` 和 `')'` 的字符串,找出最长有效(格式正确且连续)括号 子串 的长度。
// 左右括号匹配,即每个左括号都有对应的右括号将其闭合的字符串是格式正确的,比如 `"(()())"`。
// 示例 1
// 输入:s = "(()"
// 输出:2
// 解释:最长有效括号子串是 "()"
// 示例 2
// 输入:s = ")()())"
// 输出:4
// 解释:最长有效括号子串是 "()()"
// 示例 3
// 输入:s = ""
// 输出:0
//
// 提示:
// - `0 <= s.length <= 3 * 10^4`
// - `s[i]` 为 `'('` 或 `')'`
//
func longestValidParentheses(s string) int {
}
func TestLongestValidParentheses(t *testing.T) {
cases := []struct {
name string
s string
want int
}{
{"example1", "(()", 2},
{"example2", ")()())", 4},
{"example3", "", 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := longestValidParentheses(c.s)
if got != c.want {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}