-
Notifications
You must be signed in to change notification settings - Fork 1
/
22GenerateParentheses.cs
49 lines (43 loc) · 1.26 KB
/
22GenerateParentheses.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
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GenerateParentheses
{
class Program
{
static void Main(string[] args)
{
}
static ArrayList list;
public static ArrayList generateParenthesis(int n)
{
list = new ArrayList();
generateParenthesis("", 0, 0, n);
return list;
}
/**
* Latest update: July 13, 2015
* http://yyeclipse.blogspot.ca/2013/01/leetcode-generate-parentheses.html
*/
private static void generateParenthesis(String s, int left, int right, int n)
{
if (left == n && right == n)
{
list.Add(s);
return;
}
/* julia comment:
1. the way to add ) will prevent "right > left"
* 2. add ( freely if the total of it is less than n
*
* */
if (left < n)
generateParenthesis(s + "(", left + 1, right, n);
if (right < left)
generateParenthesis(s + ")", left, right + 1, n);
}
}
}