您的位置:首页 > 其它

给定一个字符串,输出所有的排列组合方式

2011-09-10 15:12 330 查看
去参加一个笔试,遇到一个问题就是给定字符串"123456"要我写程序输出所有的排列组合方式,当时头很大,一直想不出来,于是很磋的写了循环。回来了好好想了想,参考网上的资料,今天真正理解并且自己写了出来。是用递归,理解为每次都是求已知的字符串与未排列的字符串的组合!

/*
2011-9-9
author:BearFly1990
*/
package temp;

public class RecursionString {
public static void main(String[] args) {
String b = "123456";
doit("",b);
}
public static void doit(String a,String b){
if(a.length()== 6){
System.out.println(a);
}else{
for(int i = 0; i< b.length(); i++){
String tempa = new String(a);
String tempb = new String(b);
doit(tempa+tempb.charAt(i),new StringBuilder(tempb)
.deleteCharAt(i).toString());
}
}
}

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