-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrPermutation28.java
More file actions
56 lines (50 loc) · 1.32 KB
/
StrPermutation28.java
File metadata and controls
56 lines (50 loc) · 1.32 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
package offer;
import java.sql.Array;
import java.util.*;
/*
* 字符串全排列
*/
public class StrPermutation28 {
public ArrayList<String> Permutation(String str) {
ArrayList<String> list = new ArrayList<String>();
if(str.length() == 0){
return list;
}
else{
PermutationChar(str.toCharArray(), 0 , list);
// list.addAll(llist);
}
return list;
}
public void PermutationChar(char[] str, int st,
List<String> list)
{
if (st == str.length - 1)
{
if(!list.contains(String.valueOf(str))){
list.add(String.valueOf(str));
}
}
else
{
for (int i = st; i < str.length; i ++)
{
swap(str, st, i);
PermutationChar(str, st + 1, list);
swap(str, st, i);
}
}
}
public static void swap(char[] str, int i, int j)
{
char temp;
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abc";
System.out.println(new StrPermutation28().Permutation(str));
}
}