Skip to content

Commit d838f35

Browse files
committed
Add recuresive algorithm code
1 parent 6a0c38f commit d838f35

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/test/java/algorithm/recursion/BraceCombination.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.junit.Test;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
58
import static org.hamcrest.CoreMatchers.is;
69
import static org.junit.Assert.assertThat;
710

@@ -14,10 +17,30 @@ public class BraceCombination {
1417

1518
@Test
1619
public void test() {
17-
assertThat("", is(testFunction()));
20+
assertThat(null, is(괄호경우의수구하기(0)));
21+
List<String> actual = new ArrayList<>();
22+
actual.add("(())");
23+
actual.add("()()");
24+
assertThat(actual, is(괄호경우의수구하기(2)));
25+
}
26+
27+
public List<String> 괄호경우의수구하기(int n) {
28+
if (n == 0) {
29+
return null;
30+
}
31+
return combination(n, n, "", new ArrayList<>());
1832
}
1933

20-
public String testFunction() {
21-
return "";
34+
private List<String> combination(int start, int end,
35+
String pairs, List<String> result) {
36+
if (start > end) return result;
37+
if (start < 0 || end < 0) return result;
38+
if (start == 0 && end == 0) {
39+
result.add(pairs);
40+
return result;
41+
}
42+
combination(start - 1, end, pairs + "(", result);
43+
combination(start, end - 1, pairs + ")", result);
44+
return result;
2245
}
2346
}

src/test/java/algorithm/recursion/Dice.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public void test() {
1717
assertThat(4, is(calcDiceCase(3)));
1818
assertThat(8, is(calcDiceCase(4)));
1919
assertThat(16, is(calcDiceCase(5)));
20+
assertThat(32, is(calcDiceCase(6)));
21+
assertThat(63, is(calcDiceCase(7)));
22+
assertThat(125, is(calcDiceCase(8)));
2023
}
2124

2225
public int calcDiceCase(int n) {

0 commit comments

Comments
 (0)