- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPermutation.java
More file actions
Latest commit
54 lines (44 loc) · 1.31 KB
/
Copy pathPermutation.java
File metadata and controls
54 lines (44 loc) · 1.31 KB
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
53
54
packagealgorithm.recursion;
importorg.junit.Test;
importjava.util.ArrayList;
importjava.util.List;
importstaticorg.hamcrest.CoreMatchers.is;
importstaticorg.junit.Assert.assertThat;
publicclassPermutation {
/*
TASK
순열을 구한다.
*/
@Test
publicvoidtest() {
List<String> actual = newArrayList<>();
actual.add("123");
actual.add("132");
actual.add("213");
actual.add("231");
actual.add("312");
actual.add("321");
assertThat(calcPermutation("123"), is(actual));
}
publicList<String> calcPermutation(Stringstr) {
if (str == null) returnnull;
returnpermutation(str, newboolean[str.length()],
"", newArrayList<>());
}
privateList<String> permutation(Stringstr, boolean[] isPick,
Stringperm, List<String> result) {
if (str.length() == perm.length()) {
result.add(perm);
returnresult;
}
for (inti = 0; i < str.length(); i++) {
if (isPick[i]) {
continue;
}
isPick[i] = true;
permutation(str, isPick, perm + str.charAt(i), result);
isPick[i] = false;
}
returnresult;
}
}