-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.括号生成.go
65 lines (61 loc) · 1.04 KB
/
22.括号生成.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=22 lang=golang
*
* [22] 括号生成
*/
// @lc code=start
/**
* 递归 + 剪枝
*/
func generateParenthesis_1(n int) []string {
ans := []string{}
var dfs func(left, right int, cur string)
dfs = func(left, right int, cur string) {
if left == 0 && right == 0 {
ans = append(ans, cur)
return
}
if left > 0 {
dfs(left-1, right, cur+"(")
}
if right > 0 && left < right {
dfs(left, right-1, cur+")")
}
}
dfs(n, n, "")
return ans
}
/**
* BFS
*/
func generateParenthesis(n int) []string {
ans := []string{}
q := []string{"("}
for len(q) > 0 {
size := len(q)
for i := 0; i < size; i++ {
node := q[i]
if len(node) == n*2 {
ans = append(ans, node)
continue
}
left, right := 0, 0
for _, ch := range node {
if ch == '(' {
left++
} else {
right++
}
}
if left > right {
q = append(q, string(append([]byte(node), ')')))
}
if left < n {
q = append(q, string(append([]byte(node), '(')))
}
}
q = q[size:]
}
return ans
}
// @lc code=end