1、写出一个算法来生成1-n的全排列
2、已知一个长度为N的序列A,它的每个元素是1-N之间的任何一个元素,且两两不重复,称他为一个排列,写出一个算法生成该排列的字典序的下一排列。例如,A=[3 2 1 4],则下一排列为A'=[3 2 4 1],A'的下一排列为A''=[3 4 1 2],以此类推。
package suanfa;
public class ReCall {
public static void sort(String n,int begin,int end) {
char[] temp = n.toCharArray();
if (begin == end) {
System.out.println(String.valueOf(temp));
} else {
for (int i = begin; i <= end; i++) {
char temp1 = temp[begin];
temp[begin] = temp[i];
temp[i] = temp1;
sort(String.valueOf(temp), begin + 1, end);
temp[i] = temp[begin];
temp[begin] = temp1;
}
}
}
public static void main(String[] args) {
String a="123";
sort(a,0,a.length()-1);
}
}