-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationOfRecursion.java
More file actions
37 lines (34 loc) · 949 Bytes
/
PermutationOfRecursion.java
File metadata and controls
37 lines (34 loc) · 949 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
package example;
public class PermutationOfRecursion {
public static void permutation(String input ){
if(input==null) throw new IllegalArgumentException();
char[] data=input.toCharArray();
permutation(data,0);
}
public static void permutation(char[] data,int begin){//从begin开始
int length=data.length;
if(begin==length) System.out.println(data);//区间为0
for(int i=begin;i<length;i++){
if(isUnique(data, begin, i)){
swap(data,begin,i);
permutation(data,begin+1);
swap(data, begin, i);
}
}
}
private static boolean isUnique(char[] data,int begin,int end) {//不重复返回真
for(int i=begin;i<end;i++){
if(data[i]==data[end])
return false;
}
return true;
}
private static void swap(char[] data,int left,int right) {//交换
char temp=data[left];
data[left]=data[right];
data[right]=temp;
}
public static void main(String[] args) {
PermutationOfRecursion.permutation("abbewrwer");
}
}