java - 数组中的螺旋数字 - 堆栈内存溢出

标签 java arrays matrix stack overflow

我需要在螺旋数组中添加枢轴点,如下所示:

 21 22 23 24 25
 20  7  8  9 10
 19  6  1  2 11
 18  5  4  3 12
 17 16 15 14 13

I have the java code to do just that, and it works fine with a small 5x5 array like shown above... But when I test it with a large 1001x1001 array it gives me a lot of stack overflows. I don't know how to track it, I already used try and catch without success. The code is below. Does anyone have any suggestions?

public class Spiral {
    int[][] arr = new int[1001][1001];
    int counter = 1;
    public int total = 1;
    String direction = "SOUTH";
    public Spiral(){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                arr[i][j] = 0;
            }
        }
        try {
        arr[500][500] = 1;
        spiral(500, 501);
        total += arr[0][arr.length - 1];
        System.out.println(total);
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.println(x + ", " + y);
        }
    }

    public void spiral(int x, int y) {

            counter++;
            arr[x][y] = counter;

            if(x==900&&y==900)
                System.out.println("here");
            if (direction == "SOUTH") {
                if (arr[x][y - 1] != 0) {
                    if (x + 1 < arr.length)
                    spiral(x + 1, y);
                } else {
                    total += arr[x][y];
                    direction = "WEST";
                    spiral(x, y - 1);
                }
            } else if (direction == "WEST") {
                if (arr[x - 1][y] != 0) {
                    if (y - 1 >= 0) {
                        spiral(x, y - 1);
                    }
                } else {
                    total += arr[x][y];
                    direction = "NORTH";
                    spiral(x - 1, y);
                }
            } else if (direction == "NORTH") {
                if (arr[x][y + 1] != 0) {
                    if (x - 1 >= 0) {
                        spiral(x - 1, y);
                    }
                } else {
                    total += arr[x][y];
                    direction = "EAST";
                    spiral(x, y + 1);
                }
            } else if (direction == "EAST") {
                if (arr[x + 1][y] != 0) {
                    if (y + 1 < arr.length) {
                        spiral(x, y + 1);
                    }
                } else {
                    total += arr[x][y - 1];
                    direction = "SOUTH";
                    spiral(x + 1, y);
                }
            }

    }
}

最佳答案

spiral(int, int) 是递归的,并且调用自身的次数太多以至于导致堆栈溢出。您有两个选择:

  1. 重构算法以循环而不是递归
  2. 使用 vm arg -Xss 增加堆栈大小。默认情况下,虚拟机使用 512 KB 作为堆栈,因此您可以尝试使用 -Xss1m 将该大小加倍(或您可能需要的任何其他值)

关于java - 数组中的螺旋数字 - 堆栈内存溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11417966/

相关文章:

java - Eclipse 在 Mac 上打不开

java - 从 Guava CharMatcher 切换到 Regex

java - 需要写一定的方法

javascript - 如何在javascript中将数组转换为数组对象?

python - 如何读取多个图像并用它们创建 3D 矩阵?

java - 解析哪个版本的 XML 模式用于具有版本属性的 XML 文档

ios - 无法在 Swift 标准库引用中找到 enumerate() func

sql - 在 PostgreSQL 中将一行转换为数组

python - 如何使用 sympy 操作矩阵中的表达式?

javascript - 如何从转换后的楼层 map 中正确计算坐标?