题目

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语言支持的变量类型 类变量:独立于方法之外的变量,用 static 修饰. 局部变量:类的方法中的变量. 实例变量(全局变量):独立于方法之外的变量,不过没有 static 修饰. publi ...

  2. BZOJ.3139.[HNOI2013]比赛(搜索 Hash)

    题目链接 不会搜索了.. DFS()中两个参数,枚举每两个队伍的比赛结果(分配当前队伍的分数). 可以发现方案数量与具体哪只球队得了多少分无关,只与当前比赛的队伍数量和得分序列的组成有关.可以记忆化搜 ...

  3. Codeforces Round #369 (Div. 2) D. Directed Roads 数学

    D. Directed Roads 题目连接: http://www.codeforces.com/contest/711/problem/D Description ZS the Coder and ...

  4. java 实现生产者-消费者模式

    生产和消费者模式有很多种,现在介绍几种常见的方式 wait/notify实现生产和消费者模式 1.使用wait/notify实现生产和消费者模式: public class Depot { // 实际 ...

  5. Sublime Text下使用SFTP/FTP插件

    一.前言 本文主要记录了Sublime Text编辑器下的SFTP/FTP的安装使用,方便linux和windows下的文件编辑,只是简单的记录,有不足之处,还望指教. 二.Linux和windows ...

  6. 使用 IntraWeb (12) - 基本控件之 TIWGradButton、TIWImageButton

    TIWGradButton.TIWImageButton 分别是有颜色梯度变化按钮和图像按钮. TIWGradButton 所在单元及继承链: IWCompGradButton.TIWGradButt ...

  7. CentOS下的apache配置支持php

    修改Apache的配置文件httpd.conf(vi /etc/httpd/conf/httpd.conf) DirectoryIndex index.html index.php #添加index. ...

  8. java execute、executeQuery和executeUpdate之间的区别

    在用纯JSP做一个页面报警功能的时候习惯性的用executeQuery来执行SQL语句,结果执行update时就遇到问题,语句能执行,但返回结果出现问题,另外还忽略了executeUpdate的返回值 ...

  9. STM32CubeF4 FreeRTOS Examples don't work correctly with HAL_GetTick

    because the SysTick ISR has been assigned to the FreeRTOS xPortSysTickHandler() function without reg ...

  10. BTrace housemd TProfiler

    http://blog.csdn.net/y461517142/article/details/26269529 http://calvin1978.blogcn.com/articles/btrac ...