日期:2014-05-20  浏览次数:20704 次

大家来帮一下忙啊
编写带有下列声明的例程
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 TestPermute {
static long count = 0;
public static void main(String[] args) {
TestPermute tp = new TestPermute();
tp.permute( "abcdefg ");
System.out.println(count);
}

public void permute(String str) {
permute(str.toCharArray(), 0, str.length()-1);
}

private void permute(char[] str, int low, int high) {

for (int i = low; i <= high; i++) {
swap(str, low, i);
if (low + 1 == high) {
System.out.println(new String(str));
count++;
} else
permute(str, low + 1, high);
swap(str, i, low);
}
}

private void swap(char[] str, int low, int high) {
char temp = str[low];
str[low] = str[high];
str[high] = temp;
}
}