Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Lines changed: 19 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /