0022.括号生成
方法一:回溯
func generateParenthesis(n int) []string {
ans := []string{}
var backtrack func(left, right int, seqence string)
backtrack = func(left, right int, seqence string) {
if left == 0 && right == 0 {
ans = append(ans, seqence)
return
}
if left > right || left < 0 || right < 0 {
return
}
backtrack(left-1, right, seqence+"(")
backtrack(left, right-1, seqence+")")
}
backtrack(n, n, "")
return ans
}