打印螺旋阶矩阵 [英] print spiral order matrix

查看:57
本文介绍了打印螺旋阶矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个m * n个元素的矩阵(m行,n列),以螺旋顺序返回矩阵的所有元素.

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

代码不适用于大型输入,实际上我怀疑代码中的last for循环在运行1次以上时出错.

code is not working for large inputs, in fact i suspect something is wrong with last for loop in code where it is running 1 extra time.

    public int[] spiralOrder(final int[][] A) {
                int m=A.length;
                int n=A[0].length;
                int t=0, b=m-1, l=0, r=n-1, dir=0;
                int[] arr = new int[m*n];
                int p=0;
                while(t<=b && l<=r){

                    if(dir==0){
                        for(int i=l; i<=r ; i++)
                        {
                            arr[p] = A[t][i];
                            p++;
                        }
                        t++;
                        dir = 1;
                    }
                    else if(dir==1){
                        for(int j=t; j<=b; j++)
                        {
                            arr[p] = A[j][r];
                            p++;
                        }
                        dir = 2;
                        r--;
                    }
                    else if(dir==2){
                        if (r<0)break;
                        for(int i=r; i>=l; i--){
                            arr[p]=A[b][i];
                            p++;
                        }
                        dir=3;
                        b--;
                    }
                    else if(dir==3){
                        if (b<0) break;
                        for(int j = b; b>=t; j--){
                            if(j<0)break;
                            else if(j==0 & l==0)break;
                            else{
                                arr[p]=A[j][l];
                                p++;
                            }
                        }
                        dir=0;
                        l++;
                    }
                }
                return arr;
            }

输入数组:

[[150, 6, 240, 129, 168, 346, 218, 168, 309, 242, 26, 327][98, 275, 315, 389, 270, 2, 172, 100, 151, 41, 217, 176][267, 5, 324, 344, 134, 122, 229, 196, 225, 280, 200, 274][155, 320, 8, 215, 273, 291, 174, 165, 279, 26, 327, 214][207, 91, 121, 46, 125, 247, 303, 387, 214, 249, 97, 316]]

预期结果:

150 6 240 129 168 346 218 168 309 242 242 26 327 176 274 214 316 97 249 214 387 303 247 247 125 46 121 91 207 155 267 98 275 315 389 270 2 172 100 151 41 217 200 327 26 279 165 174 291273215 8 320 5 324 344 134 122 229 196 225 280

150 6 240 129 168 346 218 168 309 242 26 327 176 274 214 316 97 249 214 387 303 247 125 46 121 91 207 155 267 98 275 315 389 270 2 172 100 151 41 217 200 327 26 279 165 174 291 273 215 8 320 5 324 344 134 122 229 196 225 280

实际结果:
线程主"中的异常java.lang.ArrayIndexOutOfBoundsException:在in.main(Main.java:194)处的Solution.spiralOrder(Solution.java:16)处为60

Actual Result:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 60 at Solution.spiralOrder(Solution.java:16) at in.main(Main.java:194)

推荐答案

原因是因为您的p继续增长(p ++),甚至超过了A的容量.

The reason is because your p continues to grow (p++) even past the capacity of A.

在您的情况下,您尝试用数字填充A [60],但是A会一直到A [59],因为它有60个位置,包括A [0].

In your case you are trying to populate A[60] with a number but A only goes until A[59] because it has 60 places, including A[0].

A仅包含mn个位置,因此您必须将第一个循环设置为仅在pn时运行.

A only contains mn places, so you must condition the first loop to run only if pn.

当然,为了避免每次进行计算,您都可以将m * n保存在变量中并与p进行比较.

Of course to avoid making the calculation every time you can save m*n inside a variable and compare p with it.

if(dir==0){
   for(int i=l; i<=r&&p<m*n ; i++)
   {
      arr[p] = A[t][i];
      p++;
   }
   t++;
   dir = 1;
}

这篇关于打印螺旋阶矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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