比如我有一个数“abc”,
列出为3为数的组合
abc
acb
bac
bca
cab
cba四位数就是"abcd"就列出4*3*2*1=24个数,
如果是“abcde”就是列出5*4*3*2*1=120个数,我现在已经做出来了,但是效率很低,到6位数时就出现Exception in thread "main" java.lang.OutOfMemoryError: Java heap space的异常,大家有什么好方法么?

解决方案 »

  1.   

    期待有明白人知道,而且如果死后aabbcd这种有重复字符的值的话还需要单独判断,我暂时没写明白
      

  2.   


    /**只支持到9位
    */
    public class Main {    public static void main(String[] args) throws Exception {
            String str = "123456789";
            java.util.Vector<char[]> vc = getAll(str.toCharArray());
            System.out.println("");
            for(char[] c:vc){
                System.out.println(new String(c));
            }
        }
           static  public java.util.Vector<char[]> getAll(char []tem) {
            java.util.Vector<char[]> re = new java.util.Vector();
            /////////////////
            re.add(tem);
            char bu;
            for(int i=1,im=tem.length;i<im;i++){
                for(int k=0,km=re.size();k<km;k++){
                    char[]bt = re.get(k);
                    for(int j=0;j<i;j++){
                    char[]t = new char[bt.length];
                        System.arraycopy(bt, 0, t, 0, bt.length);
                        bu=t[j];
                        t[j]=t[i];
                        t[i]=bu;
                        re.add(t);
                    }
                }
            }
            return re;
        }
    }
      

  3.   


    我说LZ,别人回答你的问题,错了就错了,指出来就可以了吗。干嘛如此没素质呀!人家回答错了,就要被骂,你这种LZ我不伺候,谁爱回答谁回答去。真让人生气: 别出来搞IT了,回去吧,连宽容的态度都没有,难怪有人说IT人的素质差。看看LZ这种就人就行了。
      

  4.   

    拷贝一个我朋友写的。呵呵 楼主
    # import java.util.ArrayList;  
    # import java.util.Arrays;  
    #   
    # public class Permutation {  
    #   
    #     /** 
    #      * @param args 
    #      */  
    #     public static void main(String[] args) {  
    #         String[] strs=permutation("1234");  
    #         System.out.println(strs.length);  
    #   
    #     }  
    #   
    #     public  static String[] permutation(String str) {  
    #         ArrayList<String> myList = new ArrayList<String>();  
    #         char[] strChars=str.toCharArray();  
    #         char temp;  
    #         long times=1;  
    #         int pos=strChars.length-2;  
    #         int increment=-1;  
    #         for(int i=1;i<strChars.length+1;i++){  
    #             times*=i;  
    #         }  
    #         for(int i=1;i<times;i++){  
    #             temp=strChars[pos];  
    #             strChars[pos]=strChars[pos+1];  
    #             strChars[pos+1]=temp;  
    #             myList.add(new String(strChars));  
    #             pos+=increment;  
    #             if(pos==-1){  
    #                 increment=1;  
    #                 pos=0;  
    #                 temp=strChars[strChars.length-2];  
    #                 strChars[strChars.length-2]=strChars[strChars.length-1];  
    #                 strChars[strChars.length-1]=temp;  
    #                 myList.add(new String(strChars));  
    #                 i++;  
    #             }else if(pos==strChars.length-1){  
    #                 increment=-1;  
    #                 pos=strChars.length-2;  
    #                 temp=strChars[0];  
    #                 strChars[0]=strChars[1];  
    #                 strChars[1]=temp;  
    #                 myList.add(new String(strChars));  
    #                 i++;  
    #             }  
    #         }  
    #         return myList.toArray(new String[0]);  
    #     }  
    #   
    # }
      

  5.   


    public class Main {
    public static void main(String[] args) {
    String s = "abc"; 
    char[] c = s.toCharArray();
    new Main().zuhe(c, c.length, 0);
    }
    private void zuhe(char[] array, int n, int k) {
    if (n == k) {
    String str = new String(array);
    System.out.println(str);
    } else {
    for (int i = k; i < n; i++) {
    swap(array, k, i);
    zuhe(array, n, k + 1);
    swap(array, i, k);
    }
    }
    } private void swap(char[] a, int x, int y) {
    char temp = a[x];
    a[x] = a[y];
    a[y] = temp;
    }}abc
    acb
    bac
    bca
    cba
    cab