-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatent.java
More file actions
30 lines (26 loc) · 737 Bytes
/
Copy pathPatent.java
File metadata and controls
30 lines (26 loc) · 737 Bytes
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
package leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* Created by helmeter on 6/13/16.
*/
public class Patent {
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList();
dfs(ans, "", n, 0, 0);
return ans;
}
private void dfs(List<String> ans, String curr, int n, int numl, int numr) {
if (numl == n && numr == n) {
ans.add(curr);
return;
}
if (numl < n)
dfs(ans, curr+'(', n, numl+1, numr);
if (numr < numl)
dfs(ans, curr+')', n, numl, numr+1);
}
public static void main(String[] args) {
System.out.println(new Patent().generateParenthesis(3));
}
}