135 字
1 分钟
LeetCode-22. 括号生成
回溯
数字
n代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例 1:
输入:n = 3输出:["((()))","(()())","(())()","()(())","()()()"]示例 2:
输入:n = 1输出:["()"]
func generateParenthesis(n int) []string { res := []string{} path := []byte{}
var backtrack func(left, right int) backtrack = func(left, right int) { if len(path) == n * 2 { res = append(res, string(path)) return }
if left < n { path = append(path, '(') backtrack(left+1,right) path = path[:len(path)-1] }
if right < left { path = append(path, ')') backtrack(left,right+1) path = path[:len(path)-1] } }
backtrack(0,0) return res} LeetCode-22. 括号生成
https://sheep44044.github.io/posts/算法/回溯/leetcode-22-括号生成/