Skip to content

Commit 7f42680

Browse files
committed
add leetcode 22
1 parent e6684c1 commit 7f42680

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
var output []string
4+
5+
func generateParenthesis(n int) []string {
6+
output = make([]string, 0)
7+
iteration(0, 0, n, "")
8+
return output
9+
}
10+
11+
func iteration(left int, right int, max int, s string) {
12+
if left == right && left == max {
13+
output = append(output, s)
14+
return
15+
}
16+
17+
if left < max {
18+
iteration(left+1, right, max, s+"(")
19+
}
20+
if right < left {
21+
iteration(left, right+1, max, s+")")
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGenerateParenthesis(t *testing.T) {
10+
assert.Equal(t, []string{""}, generateParenthesis(0))
11+
assert.Equal(t, []string{"((()))", "(()())", "(())()", "()(())", "()()()"}, generateParenthesis(3))
12+
}

0 commit comments

Comments
 (0)