9嵌套Fors而不重复过去的数字

时间:2016-09-24 01:28:10

标签: java optimization

我正试图获得123456789的所有排列。我正在使用JTree。如果不把我的电脑烧到地上,我无法解决这个问题。这就是我所拥有的:

 //create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
//create the child nodes
for (int x=1; x<10; x++){
    DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(x);
    root.add(leaf);
    for (int x1=1; x1<9;x1++){
        DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode(x1);          
        leaf.add(leaf1);
        for (int x2=1; x2<8;x2++){
            DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode(x2);
            leaf1.add(leaf2);
            for (int x3=1; x3<7;x3++){
                DefaultMutableTreeNode leaf3 = new DefaultMutableTreeNode(x3);
                leaf2.add(leaf3);
                for (int x4=1; x4<6;x4++){
                    DefaultMutableTreeNode leaf4 = new DefaultMutableTreeNode(x4);
                    leaf3.add(leaf4);
                    for (int x5=1; x5<5;x5++){
                        DefaultMutableTreeNode leaf5 = new DefaultMutableTreeNode(x5);
                        leaf4.add(leaf5);
                        for (int x6=1; x6<4;x6++){
                            DefaultMutableTreeNode leaf6 = new DefaultMutableTreeNode(x6);
                            leaf5.add(leaf6);
                            for (int x7=1; x7<3;x7++){
                                DefaultMutableTreeNode leaf7 = new DefaultMutableTreeNode(x7);
                                leaf6.add(leaf7);
                                for (int x8=1; x8<2;x8++){
                                    DefaultMutableTreeNode leaf8 = new DefaultMutableTreeNode(x8);
                                    leaf7.add(leaf8);
                                    for (int x9=1; x9<1;x9++){
                                        DefaultMutableTreeNode leaf9 = new DefaultMutableTreeNode(x9);
                                        leaf8.add(leaf9);
                                    }
                                }
                            }   
                        }   
                    }
                }
            }
        }
    }
}

我试过if(x1!= x),但我需要将语句更改为x1&lt; 10,依此类推。有没有有效的方法呢?

1 个答案:

答案 0 :(得分:0)

您可以使用递归。您将找到此算法的解释here

public class Permutations {
    private boolean[] used;
    private StringBuilder out = new StringBuilder();
    private final String in;

    public Permutations(final String str) {
        in = str;
        used = new boolean[in.length()];
    }

    public void permute() {
        if (out.length() == in.length()) {
            System.out.println(out);
            return;
        }
        for (int i = 0; i < in.length(); ++i) {
            if (used[i])
                continue;
            out.append(in.charAt(i));
            used[i] = true;
            permute();
            used[i] = false;
            out.setLength(out.length() - 1);
        }
    }
}

这是Permutations类的使用示例:

public class Main {

    public static void main(String[] args) {
        Permutations permutation = new Permutations("123");
        permutation.permute();
    }

}

控制台输出:

123
132
213
231
312
321