-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
47 lines (42 loc) · 986 Bytes
/
Solution.java
File metadata and controls
47 lines (42 loc) · 986 Bytes
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
package charpermutation;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public ArrayList<String> Permutation(String str) {
ArrayList<String> perList = new ArrayList<>();
char[] ch = str.toCharArray();
getPermutation(ch, 0, perList);
Collections.sort(perList);
return perList;
}
/**
* getPermutation TODO :
* @param str
* @param strBegin
* @param perList
* @author zhiman
* @date 2018/02/25 ÏÂÎç7:27:13
*/
private void getPermutation(char[] ch, int strBegin, ArrayList<String> perList) {
if (ch == null) {
return;
}
if (strBegin == ch.length - 1) {
String s = String.valueOf(ch);
if (!perList.contains(s)) {
perList.add(s);
}
}
for (int i = strBegin; i < ch.length; i++) {
// ½»»»
char temp = ch[i];
ch[i] = ch[strBegin];
ch[strBegin] = temp;
getPermutation(ch, strBegin + 1, perList);
// »Ö¸´ÔÑù
temp = ch[i];
ch[i] = ch[strBegin];
ch[strBegin] = temp;
}
}
}