-
Notifications
You must be signed in to change notification settings - Fork 0
/
022.go
93 lines (83 loc) · 1.52 KB
/
022.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package p022
/**
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
*/
/**
格路问题
*/
type point struct {
x int
y int
}
func generateParenthesis(n int) []string {
ans := make([]string, 0)
path := make([]byte, n*2)
for i := 0; i < n; i++ {
path[i] = '('
}
for i := n; i < n*2; i++ {
path[i] = ')'
}
points := make([]point, 2*n-1)
points[0] = point{n, 0}
plen := 1
for {
//print and append
ans = append(ans, string(path))
// no found
if plen == 2*n-1 {
break
}
// find and then change it
for i := plen - 1; i >= 0; i-- {
p := points[i]
sum := p.x + p.y
start, index := -1, -1
if path[sum-1] == '(' {
if sum >= 2 && path[sum-2] == '(' {
bp := point{p.x - 1, p.y}
points[i] = bp
path[sum-1] = ')'
secondp := point{bp.x, bp.y + 1}
points[i+1] = secondp
thirdp := point{n, secondp.y}
points[i+2] = thirdp
start = bp.x
index = sum
plen = i + 1 + 2
}
} else {
if p.y < p.x {
bp := point{p.x, p.y + 1}
points[i] = bp
path[sum] = ')'
secondp := point{n, bp.y}
points[i+1] = secondp
start = bp.x
index = sum + 1
plen = i + 1 + 1
}
}
if start > 0 && index > 0 {
for i := start; i < n; i++ {
path[index] = '('
index++
}
for index < n*2 {
path[index] = ')'
index++
}
break
}
}
}
return ans
}