You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

螺旋矩阵Java实现问题:输出与预期不符求排查

螺旋矩阵Java实现错误排查与修正

问题背景

需要实现Java静态方法int[][] spiral(int rows, int columns),返回一个rows×columns的二维数组,从1开始以顺时针螺旋方式填充数字至rows*columns。测试时发现第三行第三列预期为29,实际输出为31,不符合预期。

原代码

static int[][] spiral(int rows, int columns) {
   int mat[][] = new int[rows][columns];
    int counter = 1;
    int startCol = 0;
    int endCol = columns - 1;
    int startRows = 0;
    int endRows = rows -1;

    while (startRows <= endRows && startCol <= endCol){
        for (int i = startCol; i <= endCol; i++){
            mat[startRows][i] = counter;
            counter++;
        }
        startRows++;

        for (int j = startRows; j <= endRows; j++){
            mat[j][endCol] = counter;
            counter++;
        }
        endCol--;

        for (int l = endCol; l >= startCol; l--){
            mat[endRows][l] = counter;
            counter++;
        }
        endRows--;

        for(int y = endRows; y >= startRows; y--){
            mat[y][startCol] = counter;
            counter++;
        }
        startCol++;
    }

    return mat;
}

预期输出

[[1;2;3;4;5;6];
[18;19;20;21;22;7];
[17;28;29;30;23;8];
[16;27;26;25;24;9];
[15;14;13;12;11;10]]

实际输出

[[1;2;3;4;5;6];
[18;19;20;21;22;7];
[17;28;31;30;23;8];
[16;27;26;25;24;9];
[15;14;13;12;11;10]]

错误分析

问题出在当矩阵只剩下单行或单列时,后续循环仍会执行,导致已填充的位置被重复覆盖。以测试用例的5行6列为例:
当循环执行到最后一轮时,startRows=2、endRows=2(仅剩下第三行),startCol=2、endCol=3。

  1. 第一个循环正常填充第三行的第3、4列(索引2、3),赋值为29、30,startRows变为3;
  2. 第二个循环因startRows(3) > endRows(2)不执行,endCol变为2;
  3. 第三个循环此时仍会执行,填充第三行的第3列(索引2),将原本的29覆盖为31,这就是错误的根源;
  4. 后续循环不再执行,但错误已经产生。

解决思路:每个填充循环执行前,先判断当前的边界是否合法,避免不必要的填充操作。

修正后的代码

static int[][] spiral(int rows, int columns) {
    int mat[][] = new int[rows][columns];
    int counter = 1;
    int startCol = 0;
    int endCol = columns - 1;
    int startRow = 0;
    int endRow = rows - 1;

    while (startRow <= endRow && startCol <= endCol) {
        // 从左到右填充顶部行
        for (int i = startCol; i <= endCol; i++) {
            mat[startRow][i] = counter++;
        }
        startRow++;

        // 从上到下填充右侧列,先判断是否还有行可填充
        if (startRow <= endRow) {
            for (int j = startRow; j <= endRow; j++) {
                mat[j][endCol] = counter++;
            }
            endCol--;
        }

        // 从右到左填充底部行,先判断是否还有列可填充
        if (startCol <= endCol) {
            for (int l = endCol; l >= startCol; l--) {
                mat[endRow][l] = counter++;
            }
            endRow--;
        }

        // 从下到上填充左侧列,先判断是否还有行可填充
        if (startRow <= endRow) {
            for (int y = endRow; y >= startRow; y--) {
                mat[y][startCol] = counter++;
            }
            startCol++;
        }
    }

    return mat;
}

验证结果

修正后执行代码,第三行第三列的值为29,与预期输出完全一致:

[[1;2;3;4;5;6];
[18;19;20;21;22;7];
[17;28;29;30;23;8];
[16;27;26;25;24;9];
[15;14;13;12;11;10]]

内容的提问来源于stack exchange,提问作者Max.ZiLvova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 12:36:14