File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change 6
6
* https://leetcode-cn.com/problems/generate-parentheses/description/
7
7
*
8
8
* algorithms
9
- * Medium (68.25%)
10
- * Total Accepted: 15K
11
- * Total Submissions: 21.9K
9
+ * Medium (70.38%)
10
+ * Likes: 385
11
+ * Dislikes: 0
12
+ * Total Accepted: 24.1K
13
+ * Total Submissions: 34.2K
12
14
* Testcase Example: '3'
13
15
*
14
16
* 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
25
27
*
26
28
*
27
29
*/
30
+
31
+ func helper (left int , right int , out string , res * []string ) {
32
+ if left > right {
33
+ return
34
+ }
35
+ if left == 0 && right == 0 {
36
+ * res = append (* res , out ) // 复制值
37
+ return
38
+ }
39
+ if left > 0 {
40
+ helper (left - 1 , right , out + "(" , res )
41
+ }
42
+ if right > 0 {
43
+ helper (left , right - 1 , out + ")" , res )
44
+ }
45
+ }
46
+
28
47
func generateParenthesis (n int ) []string {
29
-
48
+ res := []string {}
49
+ helper (n , n , "" , & res )
50
+ return res
30
51
}
31
52
You can’t perform that action at this time.
0 commit comments