Java 有没有办法做到我在这里所做的,用两个循环而不是一个循环

Java 有没有办法做到我在这里所做的,用两个循环而不是一个循环,java,Java,问题11,欧拉计划。代码工作正常,运行时间不到5秒,但时间复杂度很差。它有3个嵌套循环 背景:我的目标是,给定一个整数的二维正方形数组,我们能找到对角线上和一行中x个相邻数字的最大乘积吗 public static long leftD(int[][] square,int x){ /*This method finds the largest product of x numbers on any given left diagonal in a square */

问题11,欧拉计划。代码工作正常,运行时间不到5秒,但时间复杂度很差。它有3个嵌套循环

背景:我的目标是,给定一个整数的二维正方形数组,我们能找到对角线上和一行中x个相邻数字的最大乘积吗

public static long leftD(int[][] square,int x){
    /*This method finds the largest product of x numbers
    on any given left diagonal in a square
    */
    int rows=0,columns=0;
    int count=0;
    long product=1,largestProduct=0,Answer=0;
    int index=0;
    for(int rowN=0,colN=square[rowN].length-1;rowN<square.length-1 && colN>0;colN--){
        /*Outer for loop controls inner for loop
        makes it move across/down after each downward
        diagonal is found. colN is the column we start on and
        rowN is the row we start on.
        */
        for(rows=rowN,columns=colN;rows<(square.length-x) && columns>=x-1;rows++,columns--){
            //Above for loop gets one full diagonal
            /*Also for clarity, the length of the column equals the row #.
            The length of the diagonal equals the length of the column aka row #.
            Thus "rows" also equals the length of a given diagonal.
            */
            for(int r=rows,c=columns;c>(columns-x);c--,r++){
                int num=square[r][c];
                product=product*num;
            }
            largestProduct=(product>largestProduct)? product:largestProduct;
            product=1;
        }
        count++;
        //System.out.printf("The largest product of left diagonal is %,d on diagonal %d\n",largestProduct,count);
        product=1;
        Answer=(largestProduct>Answer)? largestProduct:Answer;
        largestProduct=0;
        if(colN==1){
            /*so if you have iterated through all diagonals
            (a diagonal has at least to terms based on how this code is made)
            of a given row, then start back from the last column, colN,(left most column)
            and let the current row, rowN, go down by one.
            */
            colN=square[rowN].length;//cause loop will imediately update this value
            //to colN--;
            rowN++;
        }
    } 
    System.out.printf("The largest product in all left diagonals is %,d\n",Answer);
    return Answer;
}

public static long R(int[][] square,int x){
    /*This method finds the largest product of x numbers
    on any given row in a square
    */
    long product=1;
    long Answer=1,count=0;
    for(int rowN=0;rowN<square.length;rowN++){//for as many rows in square

        for(int col=0;col<square[rowN].length-(x-1);col++){//for length of each row
            //-x cause we go across by x terms
            for(int i=col;i<(col+x);i++){//for the user given # x
                int number=square[rowN][i];
                product=product*number;
            }
            Answer=(product>Answer)? product:Answer;
            product=1;
        }

    }
    System.out.printf("The largest product in rows is %,d\n",Answer);
    return Answer;
}
我得到了我所期望的。答案是正确的,但我真正的问题是: 每个方法是否可能只有2个嵌套循环而不是3个。
我想这会使它更有效。

如果您想将相邻数字的数量作为参数,可能需要三个嵌套循环

请参阅下面的实现,它对我来说更清晰

公共静态int-eulerProblem11int[]mx,int-n{ int max=0,东、南、东南、西南; forint i=0;imax{ max=n; } } 返回最大值; } 另外,你还应该跟在变量答案后面→ 答复:

首字母小写的混合大小写


我很抱歉。这看起来糟透了