字符串的排列
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;
}
}
}
}
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;
}
}
}
}
相关文章推荐
- 实现一个字符串的所有排列组合
- 字符串的全排列算法讲解
- python给定n个字符串,请对n个字符串按照字典序排列
- 每天一道算法题(21)——字符串的全排列和组合算法
- 【C语言】将函数reverse_string(char * string)参数字符串中的字符反向排列。
- 编程题—字符串的排列
- 字符串全排列和组合问题
- 字符串的排列组合问题-java
- 编写一个函数reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。
- 程序员面试100题之28字符串排列
- 编写一个函数(递归实现) 实现:将参数字符串中的字符反向排列。
- 剑指offer面试题之字符串的排列
- 字符串全排列代码实现
- 60. Permutation Sequence&字符串排列&8.7Permutations without Dups
- js数组操作-找出一组按不同顺序排列的字符串的数组元素
- 字符串的全排列和组合算法
- 字符串的倒序排列
- 剑指offer 面试题28—字符串的排列
- 字符串的全排列和组合算法
- 字符串的排列组合问题