Traverse an array diagonally

后端 未结 2 1739
我寻月下人不归
我寻月下人不归 2020-12-30 06:53

I have a large array of arbitrary size. It\'s a square array. I\'m trying to grasp how to traverse it diagonally like a / rather than a \\ (what I

相关标签:
2条回答
  • 2020-12-30 07:08

    Much simpler way is to check the sum if indexes are equals to the array.length = 1; for diagonalRight and for diagonalLeft just check if i is equals j

    Example:

    digonalLeft sums \ of matrix, because (0,0) (1,1) (2,2) makes the diagonal. diagonalRight sums / of matrix, because (0+2) = (1+1) = (2+0) = 2 and 2 is the array.length - 1.

    long diagonalLeft = 0;
    long diagonalRight = 0;
    
    for (int i = 0; i < array.lenth - 1; i++) {
        for (int j = 0; j < array.length -1; j++) {
            if (i == j) digonalLeft += array[i][j];
            if (i + j == array.length - 1) diagonalRight += array[i][j];
        }    
    }
    
    0 讨论(0)
  • 2020-12-30 07:18

    Think about the coordinates of the cells:

    . 0 1 2
    0 A B C
    1 D E F
    2 G H I
    

    For any diagonal, all of the elements have something in common: the sum of an element's coordinates is a constant. Here are the constants:

    0 = 0+0 (A)
    1 = 1+0 (B) = 0+1 (D)
    2 = 2+0 (C) = 1+1 (E) = 0+2 (G)
    3 = 2+1 (F) = 1+2 (H)
    4 = 2+2 (I)
    

    The minimum constant is the smallest coordinate sum, 0. The maximum constant is the largest coordinate sum. Since each coordinate component can go up to array.length - 1, the maximum constant is 2 * (array.length - 1).

    So the thing to do is iterate over the constants. For each constant, iterate over the elements whose coordinates sum to the constant. This is probably the simplest approach:

    for (int k = 0; k <= 2 * (array.length - 1); ++k) {
        for (int y = 0; y < array.length; ++y) {
            int x = k - y;
            if (x < 0 || x >= array.length) {
                // Coordinates are out of bounds; skip.
            } else {
                System.out.print(array[y][x]);
            }
        }
        System.out.println();
    }
    

    However, that will end up iterating over a lot of out-of-bounds coordinates, because it always iterates over all possible y coordinates, even though only one diagonal contains all possible y coordinates. Let's change the y loop so it only visits the y coordinates needed for the current k.

    One condition for out-of-bounds coordinates is x < 0. Substitute the definition of x and solve:

    x < 0
    k - y < 0
    k < y
    y > k
    

    So when y > k, x will be negative. Thus we only want to loop while y <= k.

    The other condition for out-of-bounds coordinates is x >= array.length. Solve:

    x >= array.length
    k - y >= array.length
    k - array.length >= y
    y <= k - array.length
    

    So when y <= k - array.length, x will be too large. Thus we want to start y at 0 or k - array.length + 1, whichever is larger.

    for (int k = 0; k <= 2 * (array.length - 1); ++k) {
        int yMin = Math.max(0, k - array.length + 1);
        int yMax = Math.min(array.length - 1, k);
        for (int y = yMin; y <= yMax; ++y) {
            int x = k - y;
            System.out.print(array[y][x]);
        }
        System.out.println();
    }
    

    Note: I have only proven this code correct. I have not tested it.

    0 讨论(0)
提交回复
热议问题