标签:字符串全排列 sys pac string for tree i++ perm 长度
经常会遇到字符串全排列的问题。例如:输入为{‘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);
}
}
标签:字符串全排列 sys pac string for tree i++ perm 长度
原文地址:http://www.cnblogs.com/yangchunchun/p/7458262.html