This commit is contained in:
2026-07-23 16:56:50 +08:00
parent 124ad9afdf
commit 92784ffb0f
36 changed files with 2015 additions and 608 deletions
+50
View File
@@ -0,0 +1,50 @@
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)
}
})
}
}