forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaiLie.java
More file actions
48 lines (42 loc) · 1.13 KB
/
Copy pathPaiLie.java
File metadata and controls
48 lines (42 loc) · 1.13 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
package ALiBaBa;
import org.junit.Test;
import java.util.ArrayList;
/**
* @Author MaoTian
* @Classname PaiLie
* @Description TODO
* @Date 上午10:01 2019/8/17
* @Version 1.0
* @Created by mao<[email protected]>
*/
public class PaiLie {
public ArrayList<String> permutation(String s){
ArrayList<String> list=new ArrayList<>();
char[] arr=s.toCharArray();
helper(arr,0,list);
return list;
}
public void helper(char[] arr,int start,ArrayList<String> list) {
if(start==arr.length-1){
if(!list.contains(String.valueOf(arr))){
list.add(String.valueOf(arr));
}
}else{
for(int i=start;i<arr.length;i++){
char tmp=arr[start];
arr[start]=arr[i];
arr[i]=tmp;
helper(arr,start+1,list);
arr[i]=arr[start];
arr[start]=tmp;
}
}
}
@Test
public void check(){
ArrayList<String> ans=permutation("123");
for (int i = 0; i < ans.size() ; i++) {
System.out.println(ans.get(i));
}
}
}