题目

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. 快速排序之Java实现

    快速排序之Java实现 代码: package cn.com.zfc.lesson21.sort; /** * * @title QuickSort * @describe 快速排序 * @autho ...

  2. Beta冲刺准备

    过去存在的问题: 界面不够美观 推荐不够人性化 代码不够符合开闭原则 我们已经做了哪些调整/改进: 本来想引入springAndroid,但看了下google的官方文档,不建议引入第三方框架:代码重构 ...

  3. hdu 4612 边双联通 ***

    题意:有N 个点,M条边,加一条边,求割边最少.(有重边) 链接:点我 先求双连通分量,缩点形成一个生成树,然后求这个的直径,割边-直径即是答案 #pragma comment(linker, &qu ...

  4. BZOJ4277 : [ONTAK2015]Cięcie

    假设分成如下三段: [1..i][i+1..j][j+1..n] 考虑中间那一段,设f[i]为前i位组成的数模q的值,pow[i]为$10^i$模q的值,那么有: f[j]-f[i]*pow[j-i] ...

  5. 2016-2017 CT S03E06: Codeforces Trainings Season 3 Episode 6(8/13)

    2016-2017 CT S03E06: Codeforces Trainings Season 3 Episode 6 比赛连接: http://codeforces.com/gym/101124/ ...

  6. 【面试虐菜】—— JAVA面试题(2)

    前篇推荐:http://www.cnblogs.com/xing901022/p/3975626.html 1 String = 与 new 的不同 使用“=”赋值不一定每次都创建一个新的字符串,而是 ...

  7. 10分钟上手python pandas

    目录 Environment 开始 对象创建 查看数据 选择 直接选择 按标签选择 按位置选择 布尔索引 设置 缺失数据 操作 统计 应用(apply) 直方图化(Histogramming) 字符串 ...

  8. Eclipse中执行Maven命令时控制台输出乱码

    Maven 默认编码为 GBK: 在 Eclipse 控制台输出乱码: 解决方法:将以下代码添加到 pom.xml 的 <project> 节点下: <project> …… ...

  9. CentOS安装CLI

    #使用root账号 vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epe ...

  10. java 对mongodb的操作

    java 对mongodb的操作 1.1连单台mongodb Mongo mg = newMongo();//默认连本机127.0.0.1  端口为27017 Mongo mg = newMongo( ...