标签:oid new ons static list lse print contains sort
/*
输入一个字符串,按字典序打印出该字符串中字符的所有排列。
例如输入字符串abc,
则打印出由字符a,b,c所能排列出来的
所有字符串abc,acb,bac,bca,cab和cba。
*/
import java.util.*;
public class Class32 {
public ArrayList<String> printNewArray(String str){
ArrayList<String> result = new ArrayList<String>();
if(str == null || str.length() == 0){
return result;
}
if(str != null && str.length() > 0){
printNewArrayMain(str.toCharArray(),0,result);
Collections.sort(result);;
}
return result;
}
public void printNewArrayMain(char[] data, int i, ArrayList<String> list){
if(data == null || data.length == 0){
return;
}
if(i < 0 || i > data.length - 1){
return;
}
if(i == data.length - 1){
if(!list.contains(String.valueOf(data))){
list.add(String.valueOf(data));
}
}else{
for(int j = i; j < data.length; j++){
swap(data,i,j);
printNewArrayMain(data,i+1,list);
swap(data,i,j);
}
}
}
public void swap(char[] data, int i, int j){
char temp = data[i];
data[i] = data[j];
data[j] = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
标签:oid new ons static list lse print contains sort
原文地址:https://www.cnblogs.com/zhuozige/p/12512486.html