-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBraceCombination.java
46 lines (38 loc) · 1.22 KB
/
BraceCombination.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package algorithm.recursion;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class BraceCombination {
/*
TASK
N개 괄호로 만들 수 있는 모든 조합 출력하기.
*/
@Test
public void test() {
assertThat(null, is(괄호경우의수구하기(0)));
List<String> actual = new ArrayList<>();
actual.add("(())");
actual.add("()()");
assertThat(괄호경우의수구하기(2), is(actual));
}
public List<String> 괄호경우의수구하기(int n) {
if (n == 0) {
return null;
}
return combination(n, n, "", new ArrayList<>());
}
private List<String> combination(int start, int end,
String pairs, List<String> result) {
if (start > end) return result;
if (start < 0 || end < 0) return result;
if (start == 0 && end == 0) {
result.add(pairs);
return result;
}
combination(start - 1, end, pairs + "(", result);
combination(start, end - 1, pairs + ")", result);
return result;
}
}