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

去参加一个笔试,遇到一个问题就是给定字符串"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());
           }
        }
    }
  
    
}

你可能感兴趣的:(ACM)