forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT27.java
More file actions
55 lines (50 loc) · 1.48 KB
/
T27.java
File metadata and controls
55 lines (50 loc) · 1.48 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
package web; /**
* @program LeetNiu
* @description: 字符串的排列
* @author: mf
* @create: 2020/01/13 21:52
*/
import java.util.ArrayList;
import java.util.TreeSet;
/**
* 输入一个字符串,按字典序打印出该字符串中字符的所有排列。
* 例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba
*/
public class T27 {
public ArrayList<String> Permutation(String str) {
ArrayList<String> result = new ArrayList<>();
if(str == null || str.length() == 0) {
return result;
}
char[] chars = str.toCharArray();
TreeSet<String> temp = new TreeSet<>();
Permutation(chars, 0, temp);
result.addAll(temp);
return result;
}
private void Permutation(char[] chars, int begin, TreeSet<String> result) {
if (chars == null || chars.length == 0 || begin < 0 || begin > chars.length - 1) {
return;
}
if (begin == chars.length - 1) {
result.add(String.valueOf(chars));
} else {
for (int i = begin; i <= chars.length - 1; i++) {
swap(chars, begin, i);
Permutation(chars, begin + 1, result);
swap(chars, begin, i);
}
}
}
/**
* 交换
* @param x
* @param a
* @param b
*/
private void swap(char[] x, int a, int b) {
char t = x[a];
x[a] = x[b];
x[b] = t;
}
}