字符串全排列 java实现
经常会遇到字符串全排列的问题。例如:输入为{‘a’,’b’,’c’},则其全排列组合为abc,acb,bac,bca,cba,cab。对于输入长度为n的字符串数组,全排列组合为n!种。
package Bayes;
public class RecursionTree {
public static void permutation(char[] s,int from,int to) {
if(to<=1)
{
return;
}
if(from ==to)
{
System.out.println(s);
}
else
{
for (int i = from; i <=to; i++) {
swap(s, i, from);
permutation(s, from+1, to);
swap(s, from, i);
}
}
}
public static void swap(char[] s,int i,int j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
public static void main(String[] args) {
char[] s = {\'a\',\'b\',\'c\'};
permutation(s, 0, 2);
}
}
以上是 字符串全排列 java实现 的全部内容, 来源链接: utcz.com/z/392523.html