forked from xiaoningning/java-algorithm-2010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationPermutation.java
More file actions
71 lines (62 loc) · 2.05 KB
/
CombinationPermutation.java
File metadata and controls
71 lines (62 loc) · 2.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.ArrayList;
public class CombinationPermutation {
public static void main(String[] args) {
String s = "abcd";
int k = 3;
System.out.println("Combination");
combinationK("", s, k);
System.out.println("Permutation");
permutationK(s.toCharArray(), s.length(), k);
System.out.println("Permutation1");
permutationK1("", s, k);
System.out.println("MoveInOut");
move(s.toCharArray(), s.length(), new ArrayList<String>(), true);
}
public static void combinationK(String prefix, String s, int K) {
if (K == 0) {
System.out.println(prefix);
return;
}
for (int i = 0; i < s.length(); ++i)
combinationK(prefix + s.charAt(i), s.substring(i + 1), K - 1);
}
public static void permutationK1(String prefix, String s, int K) {
if (K == 0) {
System.out.println(prefix);
return;
}
for (int i = 0; i < s.length(); ++i)
permutationK1(prefix + s.charAt(i), s.substring(0,i)+s.substring(i + 1), K - 1);
}
public static void permutationK(char[] s, int n, int K) {
if (K == 0) {
for (int i = n; i < s.length; ++i)
System.out.print(s[i]);
System.out.println();
return;
}
for (int i = 0; i < n; ++i) {
swap(s, i, n - 1);
permutationK(s, n - 1, K - 1);
swap(s, i, n - 1);
}
}
public static void swap(char[] a, int left, int right) {
char tmp = a[left];
a[left] = a[right];
a[right] = tmp;
}
public static void move(char[] s, int n, ArrayList<String> result, boolean pick) {
if (n == 0) {
System.out.println(result.toString());
return;
}
move(s, n - 1, result, true);
if (pick) {
result.add(String.valueOf(s[n - 1]));
} else {
result.remove(String.valueOf(s[n - 1]));
}
move(s, n - 1, result, false);
}
}