mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
24 lines
499 B
Go
24 lines
499 B
Go
package leetcode
|
|
|
|
func generateParenthesis(n int) []string {
|
|
if n == 0 {
|
|
return []string{}
|
|
}
|
|
res := []string{}
|
|
findGenerateParenthesis(n, n, "", &res)
|
|
return res
|
|
}
|
|
|
|
func findGenerateParenthesis(lindex, rindex int, str string, res *[]string) {
|
|
if lindex == 0 && rindex == 0 {
|
|
*res = append(*res, str)
|
|
return
|
|
}
|
|
if lindex > 0 {
|
|
findGenerateParenthesis(lindex-1, rindex, str+"(", res)
|
|
}
|
|
if rindex > 0 && lindex < rindex {
|
|
findGenerateParenthesis(lindex, rindex-1, str+")", res)
|
|
}
|
|
}
|