Skip to content

Commit f95b941

Browse files
author
Yeqi Tao
committed
Add Soulution.go for 0022.Generate Parentheses
1 parent 8d2d02a commit f95b941

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func generateParenthesis(n int) []string {
2+
result := make([]string, 0)
3+
backParenthesis(&result, "", 0, 0, n)
4+
return result
5+
}
6+
7+
func backParenthesis(result *[]string, cur string, open, close, max int) {
8+
if len(cur) == 2 * max {
9+
*result = append(*result, cur)
10+
return
11+
}
12+
13+
if open < max {
14+
backParenthesis(result, cur+"(", open+1, close, max)
15+
}
16+
if close < open {
17+
backParenthesis(result, cur+")", open, close+1, max)
18+
}
19+
}

0 commit comments

Comments
 (0)