编写带有下列声明的例程:第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。

2023-05-17,,

全排列在笔试面试中很热门,因为它难度适中,既可以考察递归实现,又能进一步考察非递归的实现,便于区分出考生的水平。所以在百度和迅雷的校园招聘以及程序员和软件设计师的考试中都考到了,因此本文对全排列作下总结帮助大家更好的学习和理解。对本文有任何补充之处,欢迎大家指出。

/**
* 编写带有下列声明的例程
* public void permute(String str);
private void permute(char[] str, int low, int high);
第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。
例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。
*/
public class PermuteTest{

public static void main(String[] args){
permute("abc");
}

public static void permute(String str){
char[] ch = str.toCharArray();
permute(ch, 0, ch.length-1);
}

private static void permute(char[] str, int low, int high){
int length = str.length;
if(low == high){
String s = "";
for(int i=0;i<length;i++){
s += str[i];
}
System.out.println(s);
}
for(int i=low; i<length;i++){
swap(str, low, i);
permute(str, low+1, high);
swap(str, low, i);
}
}

public static void swap(char[] str, int m, int n){
char temp = str[m];
str[m] = str[n];
str[n] = temp;
}

public static int is_swap(char[] str, int m, int n){
int flag = 1;
for(int i=m;i<n;i++){
flag = 1;
if(str[i] == str[n]){
flag = 0;
break;
}
}
return flag;
}
}

参考: http://blog.csdn.net/vino8to24/article/details/52583526

编写带有下列声明的例程:第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。的相关教程结束。

《编写带有下列声明的例程:第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。.doc》

下载本文的Word格式文档,以方便收藏与打印。