您的位置:首页 > 其它

题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。

2011-10-23 16:52 981 查看
题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。

http://zhedahht.blog.163.com/blog/static/254111742007499363479/

/**
*
*/
package interview;

import java.util.Arrays;

/**
* @author clydelou
*
*/
public class Test {

/**
* @param args
*/
public static void p(int[] a, int index) {
if (a == null || index < 0)
return;
if (index == (a.length - 1))
System.out.println(Arrays.toString(a));

else {
for (int i = index; i < a.length; i++) {
int temp = a[i];
a[i] = a[index];
a[index] = temp;

p(a, index + 1);

temp = a[i];
a[i] = a[index];
a[index] = temp;

}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 1, 2, 3 };
p(a, 0);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐