遍历数组对角线 [英] Traverse an array diagonally

查看:201
本文介绍了遍历数组对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大阵任意大小的。这是一个方阵。我试图掌握如何遍历其对角线像 / 而非 \\ (我已经知道如何去做)。我有以下的code迄今:

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 already know how to do). I have the following code thus far:

char[][] array = new char[500][500];
//array full of random letters
String arrayLine = "";
for (int y = 0; y < array.length; y++) {
    for (int x = 0; x < array.length; x++) {
        for (???) {
            arrayLine = arrayLine + array[???][???];
        }
    }
    System.out.println(arrayLine);
}

我有三个回路,因为这是我怎么做的另一条对角线:

I have three loops, because this is how I did the other diagonal:

for (int y = 0; y < array.length; y++) {
    for (int x = 0; x < array.length; x++) {
        for (int z = 0; z < array.length-y-x; z++) {
            arrayLine = arrayLine + array[y+z][x+z];
        }
    }
    System.out.println(arrayLine);
}

在我的努力,我继续下去的边界之外,并得到一个ElementOutOfBounds例外。说数组如下(3x3的,而不是500×500):

In my attempts, I keep going outside the boundaries and get an ElementOutOfBounds exception. Say the array is as below (a 3x3 instead of 500x500):

A B C
D E F
G H I

我要打印出以下字符串:

I want to print out the following as strings:

A
BD
CEG
FH
I

一个previous SO问题曾与整型数组类似的问题,而解决方案是基于关闭数组元素的总和。但我有个字符的工作,所以我不能想到一个方法来得到它。

A previous SO question had a similar problem with integer arrays, and the solution is based off of the sum of array elements. But I'm working with chars, so I can't think of a methodology to get it.

推荐答案

想想细胞的坐标:

. 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)

最小不变的是最小的坐标和,0.1最大恒是国内最大的坐标和。由于每个坐标分量可以达到 array.length - 1 ,最大常数 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();
}

不过,这最终会遍历了很多超出边界坐标,因为它在所有可能的坐标总是迭代,即使只有一个对角线包含所有可能的坐标。让我们修改循环,因此只能参观坐标所需电流 K

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.

对于超出边界坐标的一个条件是 X&LT; 0 。替换 X 并解决的定义:

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

所以,当 Y'GT;氏/ code>, X 将是负面的。因此,我们只想循环,而 Y'LT; = K

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

其他条件超出边界坐标为 X&GT; = array.length 。解决:

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

所以,当 Y'LT; = K - array.length X 将过大。因此,我们要在0或来启动 K - array.length + 1 ,以较大者为准。

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();
}

请注意:我只证明了这code正确。我没有测试它。

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

这篇关于遍历数组对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆