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].


【题目分析】

给定一个m行n列的矩阵M,求出这个矩阵螺旋形遍历的序列。


【思路】

我们是否能用控制下标来控制访问矩阵数据的顺序呢?我发现有点复杂~所以想一圈一圈来对矩阵进行序列化,这也是我首先想到的一个比较容易理解的方法。

1. 首先我们确定一个矩阵可以被遍历的圈数:circlenum = Math.min((m+1)/2, (n+1)/2);

2. 对于给定的一圈,从按顺序进行遍历:遍历最上边一行,最右边一行,最下面一行,最左边一行;

  如左图所示,先遍历最外边一圈,在遍历里面的一圈。


【java代码】

 public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<Integer>();
if(matrix == null || matrix.length == 0) return list;
//确定需要遍历的圈数
int circlenum = Math.min((matrix.length+1)/2, (matrix[0].length+1)/2);
//对每一圈进行序列化
for(int i = 0; i < circlenum; i++){
getOutCircle(list, matrix, i);
} return list;
} public void getOutCircle(List<Integer> list, int[][] matrix, int num){
int rownum = matrix.length - num*2; //确定这一圈的行数
int colnum = matrix[0].length - num*2; //确定这一圈的列数 for(int j = 0; j < colnum; j++) list.add(matrix[num][num+j]);
if(rownum <= 1) return; //如果只有一行
for(int i = 1; i < rownum; i++) list.add(matrix[num + i][num+colnum-1]);
if(colnum <= 1) return; //如果只有一列
for(int j = 1; j < colnum; j++) list.add(matrix[num+rownum-1][num+colnum-j-1]);
for(int i = 1; i < rownum - 1; i++) list.add(matrix[num+rownum-1-i][num]);
}
}

上面代码虽然可以解决问题,但是可读性不强,而且通过圈来对矩阵进行遍历,一些下标控制会让大家很懵逼,下面是一个简单的版本,很容易理解。

 public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if(matrix.length == 0 || matrix[0].length == 0) return res; int top = 0;
int bottom = matrix.length-1;
int left = 0;
int right = matrix[0].length-1; while(true){
for(int i = left; i <= right; i++) res.add(matrix[top][i]);
top++;
if(left > right || top > bottom) break; for(int i = top; i <= bottom; i++) res.add(matrix[i][right]);
right--;
if(left > right || top > bottom) break; for(int i = right; i >= left; i--) res.add(matrix[bottom][i]);
bottom--;
if(left > right || top > bottom) break; for(int i = bottom; i >= top; i--) res.add(matrix[i][left]);
left++;
if(left > right || top > bottom) break;
} return res;
}
}

LeetCode OJ 54. Spiral Matrix的更多相关文章

  1. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  2. LeetCode OJ 59. Spiral Matrix II

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

  3. 【一天一道LeetCode】#54. Spiral Matrix

    一天一道LeetCode系列 (一)题目 Given a matrix of m x n elements (m rows, n columns), return all elements of th ...

  4. 【LeetCode】54. Spiral Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  5. 【leetcode】54.Spiral Matrix

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

  6. LeetCode OJ:Spiral Matrix(螺旋矩阵)

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

  7. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  8. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  9. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. find中的-print0和xargs中-0的区别

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...

  2. auDemo

    Option Explicit Private Const MODULE_NAME = "auDemo.WSC" Dim oEccomOperation,dbDemo Set oE ...

  3. spring使用jdbcTemplate和jdbcdaosupport和namedparameter

    jdbcTemplate: 首先连接数据库 <!-- 导入外部文件 --> <context:property-placeholder location="classpat ...

  4. VM10下Ubuntu无法安装vim的问题

    今天在VM10下重装了Ubuntu14.10,安装vim时发现如下问题 谷歌了一下,终于找到一种方法解决. 终端下: sudo vi /etc/apt/source.list 该命令将用vi打开一个文 ...

  5. Design Pattern - Strategy

    Strategy Pattern:     The Strategy Pattern defines a family of algorithms,encapsulates each one,and ...

  6. 线性规划?数学?差分约束?Good Bye 2016 C

    http://codeforces.com/contest/750/problem/C 反正我不会这道题...为什么那么多人做出来了...我好菜.jpg 题目大意:cf每个人都有分数,每次都会在div ...

  7. Fighting For 2017 Season Contest 1

    比赛地址[https://vjudge.net/contest/147011#problem/A].960626 题目一:[http://codeforces.com/problemset/probl ...

  8. 第四章 android 命名规范和编码规范

    书里面讲的比较常见,单个人也是有不同的观点: 因为android绝大部分使用java开发的,因此java相关规范适用于android: Google Style: 英文地址:http://google ...

  9. win10下安装Django

    Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本. 我的电脑是操作系统是window10 ,内存是4G. 1.下载django 官网地址:https://www.dj ...

  10. uva 12356 Army Buddies

    简单的并查集应用. #include<stdio.h> #include<string.h> #include<math.h> #include<algori ...