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

本题递归思想:划分,递归求解

 

public static  ArrayList Permutation(String str){
        ArrayList resultlist  = new ArrayList();

        if(str.length()==0 || str==null){
            return (ArrayList)resultlist;
        }

        char[]  chars = str.toCharArray();
        findsublist(chars,resultlist,0);

        Collections.sort(resultlist);
        return (ArrayList)resultlist;
    }
     public static  void findsublist(char[] a, List list,int i){
        //递归终止条件
       if(i==a.length-1){
            if(!list.contains(new String(a)))
              list.add(new String(a));
              return ;

       }else{
           //递归
             for (int j = i; j 

   

你可能感兴趣的:(剑指offer)