题目

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

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]

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

题解
这道题是实现题。

考虑2个初始条件,如果矩阵只有一行或者一列,那么无需转圈,依次输出即可。

其他情况均需转圈:从左到右,从上到下,从右到左,从下到上。 从大圈依次循环到小圈即可。

代码如下:

 1    public ArrayList<Integer> spiralOrder(int[][] matrix) {

 2         ArrayList<Integer> result = new ArrayList<Integer>();

 3         if(matrix == null || matrix.length == 0)

 4             return result;

 5  

 6         int m = matrix.length;

 7         int n = matrix[0].length;

 8  

 9         int x=0; 

         int y=0;

  

         while(m>0 && n>0){

  

             //if one row/column left, no circle can be formed

             if(m==1){

                 for(int i=0; i<n; i++){

                     result.add(matrix[x][y++]);

                 }

                 break;

             }else if(n==1){

                 for(int i=0; i<m; i++){

                     result.add(matrix[x++][y]);

                 }

                 break;

             }

  

             //below, process a circle

  

             //top - move right

             for(int i=0;i<n-1;i++)

                 result.add(matrix[x][y++]);

  

             //right - move down

             for(int i=0;i<m-1;i++)

                 result.add(matrix[x++][y]);

  

             //bottom - move left

             for(int i=0;i<n-1;i++)

                 result.add(matrix[x][y--]);

  

             //left - move up

             for(int i=0;i<m-1;i++)

                 result.add(matrix[x--][y]);

  

             x++;

             y++;

             m=m-2;

             n=n-2;

         }

  

         return result;

     }

Reference:http://www.programcreek.com/2013/01/leetcode-spiral-matrix-java/

Spiral Matrix leetcode java的更多相关文章

  1. Spiral Matrix -- LeetCode

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

  2. Search a 2D Matrix leetcode java

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  3. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  4. Spiral Matrix II leetcode java

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  5. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  6. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  7. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  8. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. [LeetCode] Spiral Matrix 螺旋矩阵

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

随机推荐

  1. JDBC之 大数据内容的传输

    JDBC之 大数据内容的传输 什么是大数据内容? 在数据库中,有一条一条的记录,记录中很多字段都是几个字符就够的,假如现在要把一部小说存入数据库,这本小说当然不是几个字符组成,而是由几万字组成,这本小 ...

  2. 牛客网某比赛 I 小乐乐学博弈 博弈论

    题目大意: 有两堆石子\(n\)和\(m\),每次可以拿\(1 \sim k\)个 \(k >= |n - m|\) 问先手必胜? 把限制条件去掉才有意思 首先考虑两堆相等,那么先手怎么操作,后 ...

  3. 什么是 "use strict"? 使用它的好处和坏处分别是什么?

    ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. 设立"严格模式&q ...

  4. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

    Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...

  5. 在ASP.NET Web API中使用OData的Containment

    通常情况下,一个OData的EDM(Entity Data Model)在配置的时候定义了,才可以被查询或执行各种操作.比如如下: builder.EntitySet<SomeModel> ...

  6. DirectX - dds图片格式(DDSURFACEDESC2)

    DDS是DirectDraw Surface的缩写,它是DirectX纹理压缩(DirectX Texture Compression,简称DXTC)的产物. DXTC减少了纹理内存消耗的50%甚至更 ...

  7. C#编程(五十三)----------字典Dictionary<TKey,TValue>

    字典 关键字:Dicitionary 说明: 必须包含命名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由两个元组组成:键和值). 键必须 ...

  8. vs2010 :0X80041FEB 程序集无法修改版等内容

    AssemblyInfo.cs内容被清空或则格式有问题,无法修改,一个问题搞了8个小时  gisoracle 2018.09.22 包括加入现在项目,加入form多,都不正常,不能显示,一次稍加几个

  9. 高通与MTK瓜分天下?手机处理器品牌分析

    http://mobile.pconline.com.cn/337/3379352.html [PConline 杂谈]如果你向朋友请教买一台怎样的台式机或者笔记本的话,很多时候那朋友会根据你对电脑的 ...

  10. HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean arg0)理解

    request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个 reqeust.getSession(fa ...