-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path022-GenerateParentheses.cs
36 lines (32 loc) · 1021 Bytes
/
022-GenerateParentheses.cs
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
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 32.7 MB
// Link: https://leetcode.com/submissions/detail/378567461/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _022_GenerateParentheses
{
public IList<string> GenerateParenthesis(int n)
{
var results = new List<string>();
if (n > 0) { GenerateParenthesis(n, n, results, ""); }
return results;
}
void GenerateParenthesis(int l, int r, IList<string> results, string s)
{
if (l == 0)
{
s += new string(')', r);
results.Add(s);
return;
}
GenerateParenthesis(l - 1, r, results, s + "(");
if (r > l)
{
GenerateParenthesis(l, r - 1, results, s + ")");
}
}
}
}