Files
leetcode/top-100-liked/59/leetcode_test.go
T
2026-07-03 10:27:25 +08:00

49 lines
744 B
Go

package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/generate-parentheses/description/?envType=study-plan-v2&envId=top-100-liked
func generateParenthesis(n int) []string {
res := []string{}
r := []byte{}
num_l := 0
num_r := 0
var helper func()
helper = func() {
if num_l < n {
r = append(r, '(')
num_l++
helper()
num_l--
r = r[:len(r)-1]
}
if num_l > num_r {
r = append(r, ')')
num_r++
helper()
num_r--
r = r[:len(r)-1]
}
if num_l == n && num_r == n {
res = append(res, string(r))
}
}
helper()
return res
}
func Test(t *testing.T) {
fmt.Printf("%+v\n", generateParenthesis(3))
}
func Test2(t *testing.T) {
fmt.Printf("%+v\n", generateParenthesis(1))
}