-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathNBitWays.java
52 lines (43 loc) · 1.27 KB
/
NBitWays.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
47
48
49
50
51
52
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 NBitWays {
/*
TASK
n비트의 모든 경우의 수를 출력한다.
*/
@Test
public void test() {
List<String> actual1 = new ArrayList<>();
actual1.add("00");
actual1.add("01");
actual1.add("10");
actual1.add("11");
assertThat(calcBitCombination(2), is(actual1));
List<String> actual2 = new ArrayList<>();
actual2.add("000");
actual2.add("001");
actual2.add("010");
actual2.add("011");
actual2.add("100");
actual2.add("101");
actual2.add("110");
actual2.add("111");
assertThat(calcBitCombination(3), is(actual2));
}
public List<String> calcBitCombination(int n) {
return bitCombination(n, "", new ArrayList<>());
}
private List<String> bitCombination(int n, String str, List<String> list) {
if (n == str.length()) {
list.add(str);
return list;
}
bitCombination(n, str + "0", list);
bitCombination(n, str + "1", list);
return list;
}
}