1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| // @Title: 括号生成 (Generate Parentheses)
// @Author: 15816537946@163.com
// @Date: 2020-11-03 08:38:49
// @Runtime: 0 ms
// @Memory: 2.8 MB
func generateParenthesis(n int) []string {
var ans []string
var dfs func(string, int, int)
dfs = func(s string, left, right int) {
if left > right {
return
}
if left == 0 && right == 0 {
ans = append(ans, s)
}
if left > 0 {
dfs(s+"(", left-1, right)
}
if right > 0 {
dfs(s+")", left, right-1)
}
}
dfs("", n, n)
return ans
}
|