forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
30 lines (29 loc) · 913 Bytes
/
Solution.java
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 GenerateParentheses;
import java.util.*;
/**
* Created by Luyz on 16/8/23.
* https://leetcode.com/problems/generate-parentheses/
*/
public class Solution {
public List<String> generateParenthesis(int n) {
return generate("", n, 0);
}
public List<String> generate(String currentState,int leftRemain,int rightRemain){
List<String> result = new ArrayList<>();
if (leftRemain == 0){
if (rightRemain != 0) {
return generate(currentState + ")", 0, rightRemain - 1);
}
else {
result.add(currentState);
}
}
else {
result = generate(currentState + "(", leftRemain - 1, rightRemain + 1);
if (rightRemain > 0){
result.addAll(generate(currentState + ")", leftRemain, rightRemain - 1));
}
}
return result;
}
}