Java矩阵运行时错误 [英] Java matrix runtime error

查看:165
本文介绍了Java矩阵运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

练习信:

给定mxn元素矩阵(m行,n列),以螺旋形式返回矩阵的所有元素订单。

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

例如,给定以下矩阵:

[
     [ 1, 2, 3 ],
     [ 4, 5, 6 ],
     [ 7, 8, 9 ]
    ]
    You should return [1,2,3,6,9,8,7,4,5].

给定代码:

public class Solution {
  public List<Integer> spiralOrder(int[][] matrix) {
  }
}

我的代码:

public List<Integer> spiralOrder(int[][] matrix) {
        if(matrix == null || (matrix.length == 0))
            return new ArrayList<Integer>();
        int arriba = 0;
        int derecha = matrix[0].length - 1;
        int abajo = matrix.length - 1;
        int izquierda = 0;
        List<Integer> retorno = new ArrayList<Integer>();
        while(true)
        {
            for(int i = izquierda; i <= derecha; i++)
                retorno.add(matrix[arriba][i]);
            arriba++;
            for(int i = arriba; i <= abajo; i++)
                 retorno.add(matrix[i][derecha]);
            derecha--;
            for(int i = derecha; i >= izquierda; i--)
                retorno.add(matrix[abajo][i]);
            abajo--;
            for(int i = abajo; i >= arriba; i--)
                retorno.add(matrix[i][izquierda]);
            izquierda++;
            if(izquierda >= derecha)
                return retorno;
        }
    }
}

错误:

Runtime Error Message:
Line 13: java.lang.ArrayIndexOutOfBoundsException: 1
Last executed input:
[[1,2,3,4,5,6,7,8,9,10]]

有什么建议吗?我真的不知道出了什么问题。为什么它超出范围?
练习可以在这里找到

Any suggestions? I can't really tell what is wrong. Why is it out of bounds? Exercise can be found here

推荐答案

我用这个矩阵尝试了你的方法:

I tried your method with this matrix:

int[][] matrix = {{1,2,3},
                  {2,3,4},
                  {3,4,5}};

我没有得到任何 ArrayIndexOutOfBoundsException 。您的代码似乎没有任何错误。

and I did not get any ArrayIndexOutOfBoundsException. Your code does not seem to throw any errors.

但是,我注意到输出不符合预期。它给我的输出是 12345432 (只有8个数字),在矩阵中间缺少数字 3

However, I noticed that the output is not as expected. The output it gave me was 12345432 (only 8 numbers), missing the number 3 in the middle of the matrix.

仔细查看代码后,我意识到错误在于 if(izquierda> = derecha)。如果您将此更改为 if(izquierda> derecha),则不会错过 3 。出于同样的原因,您还需要检查 arriba> abajo ,否则你的程序不适用于任何列多于行的矩阵。

After having a thorough look at your code I realised that the error lies in if(izquierda >= derecha). If you change this to if(izquierda > derecha) it will not miss the 3. For the same reason you did this, you need to also check for arriba > abajo, otherwise your program does not work for any matrix that has more columns than rows.

编辑:每次for循环后都需要进行这些检查。

You need these checks after every for-loop.

I建议你在while循环之外移动 return retorno; ,并在支票中插入 break

I suggest you move the return retorno; outside the while-loop, and insert break in the checks:

public List<Integer> spiralOrder(int[][] matrix) {
    if(matrix == null || (matrix.length == 0))
        return new ArrayList<Integer>();
    int arriba = 0;
    int derecha = matrix[0].length - 1;
    int abajo = matrix.length - 1;
    int izquierda = 0;
    List<Integer> retorno = new ArrayList<Integer>();
    while(true)
    {
        for(int i = izquierda; i <= derecha; i++)
            retorno.add(matrix[arriba][i]);
        arriba++;
        if(arriba > abajo)
            break;

        for(int i = arriba; i <= abajo; i++)
             retorno.add(matrix[i][derecha]);
        derecha--;
        if(izquierda > derecha)
            break;

        for(int i = derecha; i >= izquierda; i--)
            retorno.add(matrix[abajo][i]);
        abajo--;
        if(arriba > abajo)
            break;

        for(int i = abajo; i >= arriba; i--)
            retorno.add(matrix[i][izquierda]);
        izquierda++;
        if(izquierda > derecha)
            break;
    }
    return retorno;
}

您的代码说明(按要求):想象一下,你有一个矩阵,四个人站在它周围 - 每个人都站在一边。这四个人被称为 arriba derecha abajo ,和 izquierda

Explanation of your code (by request): Imagine you have a matrix with four persons standing around it - each on one side. These four persons are called arriba, derecha, abajo, and izquierda:

           arriba
          1 2 3 4 5
izquierda 2 3 4 5 6 derecha
          3 4 5 6 7
            abajo

这四个人可以在他们面前看到数字行:

These four people can see the line of numbers right in front of them:


  • arriba 1 2 3 4 5

  • derecha 看到 5 6 7

  • abajo 看到 3 4 5 6 7

  • izquierda 看到 1 2 3

  • arriba sees 1 2 3 4 5.
  • derecha sees 5 6 7.
  • abajo sees 3 4 5 6 7.
  • izquierda sees 1 2 3.

每当这些人面前的所有数字都被添加到列表 retorno ,他们向前迈出了一步。例如,在第一个for循环之后,它看起来像这样:

Whenever all the numbers in front of any of these persons are added to the list retorno, they jump forward one step. For example after the first for-loop, it looks like this:

          1 2 3 4 5
           arriba
izquierda 2 3 4 5 6 derecha
          3 4 5 6 7
            abajo

在while循环的第一次迭代之后,它们是这样的:

After the whole first iteration of the while-loop, they stand like this:

1           2 3 4         5
           arriba
2 izquierda 3 4 5 derecha 6
            abajo
3           4 5 6         7




  • arriba 正在向下移动。

  • derecha 向左移动。

  • abajo 正在向上移动。

  • izquierda 向右移动。

    • arriba is moving downwards.
    • derecha is moving to the left.
    • abajo is moving upwards.
    • izquierda is moving to the right.
    • 一旦这两个人中的任何相互传递,你知道它们之间没有数字,你需要立即停止循环 。这就是为什么你需要检查两个人是否已经每次有人采取步骤(每次循环之后)。

      As soon as any of these two persons pass each other, you know that there are no numbers in between them and you need to stop the loop immediately. This is why you need to check if two persons have passed each other every time someone takes a step (after every for-loop).

      这篇关于Java矩阵运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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