Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更好地实现这一点,以最大限度地提高性能和效率(用于循环)_Java_Nested Loops - Fatal编程技术网

Java 如何更好地实现这一点,以最大限度地提高性能和效率(用于循环)

Java 如何更好地实现这一点,以最大限度地提高性能和效率(用于循环),java,nested-loops,Java,Nested Loops,我有这些嵌套for循环,有什么方法可以使它更有效吗 我想要这些数字的每一个组合。例如2,290,29091993..等 组合只是一个字符串数组列表 int[] combo = new int[]{2, 9, 0, 9, 1, 9, 9, 3}; for (int i = 0; i < combo.length; i++) { combinations.add(combo[i] + ""); for (int x = 0; x < combo.length; x++)

我有这些嵌套for循环,有什么方法可以使它更有效吗

我想要这些数字的每一个组合。例如2,290,29091993..等 组合只是一个字符串数组列表

int[] combo = new int[]{2, 9, 0, 9, 1, 9, 9, 3};
for (int i = 0; i < combo.length; i++) {
    combinations.add(combo[i] + "");
    for (int x = 0; x < combo.length; x++) {
        combinations.add(combo[i] + "" + combo[x]);
        for (int y = 0; y < combo.length; y++) {
            combinations.add(combo[i] + "" + combo[x] + "" + combo[y]);
            for (int z = 0; z < combo.length; z++) {
                combinations.add(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z]);
                for (int z1 = 0; z1 < combo.length; z1++) {
                    combinations.add(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z] + "" + combo[z1]);
                    for (int z2 = 0; z2 < combo.length; z2++) {
                        combinations.add(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z] + "" + combo[z1] + "" + combo[z2]);
                        for (int z3 = 0; z3 < combo.length; z3++) {
                            combinations.add(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z] + "" + combo[z1] + "" + combo[z3]);
                        }
                    }
                }
            }
        }
    }
}
int[]combo=newint[]{2,9,0,9,1,9,9,3};
for(int i=0;i
void addPermutation(列表组合、int级别、int[]组合、,
StringBuilder缓冲区){
整数长度=0;
if(缓冲区!=null){
length=buffer.length();
}否则{
缓冲区=新的StringBuilder();
}
如果(级别<组合长度){
for(int x:combo){
append(Integer.valueOf(x));
字符串当前值=buffer.toString();
//System.out.println(当前);XXX用于调试
组合。添加(当前);
添加置换(组合,级别+1,组合,缓冲);
buffer.setLength(长度);
}
}
}
@试验
公共无效测试置换(){
列表组合=新建LinkedList();
int[]combo=newint[]{2,9,0};
addPermutation(组合,0,组合,null);
}

当然,因为它的时间复杂度很高,所以它不能创造奇迹。

你在寻找组合或置换吗?用什么标准更有效?您想增加或减少什么?您正在重做所有这些字符串连接,保存它们并在较低级别使用它们。i、 e combo[i]+“”+combo[x]是重用的,combo[i]+“”+combo[x]+“”+combo[y]也是重用的,等等……我想你可以递归地这样做。。。但这并不一定会更有效率。我的建议是在开始时对数字数组进行排序,因为这样就不难避免重复,并且可以节省一些计算时间。此外,您可以使用数字数组列表进行组合,这样您就可以避免像“02”这样的事情,但您必须用另一种方法连接数字。
void addPermutation(List<String> combinations, int level, int[] combo,
        StringBuilder buffer) {
    int length = 0;
    if (buffer != null) {
        length = buffer.length();
    } else {
        buffer = new StringBuilder();
    }
    if (level < combo.length) {
        for (int x : combo) {
            buffer.append(Integer.valueOf(x));
            String current = buffer.toString();
            //System.out.println(current); XXX for debug purpose
            combinations.add(current);
            addPermutation(combinations,level+1,combo,buffer);
            buffer.setLength(length);
        }
    }
}

@Test
public void testPermutation(){
    List<String> combinations = new LinkedList<String>();
    int[] combo = new int[]{2, 9, 0};
    addPermutation(combinations, 0, combo, null);
}