您的位置:首页 > 编程语言 > Java开发

字符串的排列

2017-08-08 11:04 357 查看
输入一个字符数组,打印出该字符数组中字符的所有不重复的排列。

import java.util.ArrayList;
import java.util.List;

public class Main {

static List<String> lstchs = new ArrayList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] chs = {'F','G','J','J','H','K','S','L'};
lstchs.clear();
Permutation(chs);
for(int i = 0; i<lstchs.size();i++) {
System.out.println(lstchs.get(i).toString());
}
}

public static void Permutation(char[] chs) {
if(chs.length == 0)
return ;
Permutation(chs,0,0,chs.length);
}

public static void Permutation(char[] chs,int pBegin,int pstr,int len) {
if(pBegin == len) {
String s = "";
int i = 0;
while(i<len) {
s += chs[i];
i += 1;
}
if(lstchs.contains(s) == false)
lstchs.add(s);
}
else {
for(int index=pBegin;index<len;index++) {
char temp = chs[index];
chs[index] = chs[pBegin];
chs[pBegin] = temp;
Permutation(chs,pBegin+1,pstr,len);
temp = chs[index];
chs[index] = chs[pBegin];
chs[pBegin] = temp;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息