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. C#如何实现url短地址?C#短网址压缩算法与短网址原理入门

    c# url短地址压缩算法与短网址原理的例子,详细介绍了短网址的映射算法,将长网址md5生成32位签名串,分为4段,每段8个字节,然后生成短网址,具体见文本实例. 短网址映射算法: 将长网址md5生成 ...

  2. VS生成桌面应用程序

    1.简介 1/ 什么是WPF WPF,Windows Presentation Foundation也,译过来就是"Windows呈现基础",你看它的目的非常明确,就是用来把数据& ...

  3. css学习之 display:inline-block;

    设置display:inline-block;后的元素 就是一个格式化为行内元素的块容器( Block container ):通俗讲就是:将对象呈递为内联对象,但是对象的内容作为块对象呈递.旁边的内 ...

  4. 2.开启TFTP,NFS,SAMBA,SSH服务

    一.TFTP (1)dpkg -s tftp-hpa查看服务器端是否安装 (2)如果没安装 sudo apt-get install tftpd-hpa sudo apt-get install tf ...

  5. c++中string类型可以直接进行比较

    以下代码在Ubuntu14.10下实现 /*------------------------- filename is demo.cpp --------------------------*/ #i ...

  6. USACO 3.3 TEXT Eulerian Tour中的Cows on Parade一点理解

    Cows on Parade Farmer John has two types of cows: black Angus and white Jerseys. While marching 19 o ...

  7. win32下Socket编程(转载)

    在网上找了很多的资料,现将这些资料整合起来,详细介绍一下VC下的socket编程,并提供一个服务器客户端具体的实例.希望对您有所帮助 一.原理部分 (个人觉得这篇写的可以,所以转与此,原文地址:htt ...

  8. python 之遍历目录树(可匹配输出特定后缀的文件)

    涉及到的模块有os, fnmatch:1.通过os模块中的方法获取dir.subdir.files,通过os.path.join可拼接成完整路径: 2.fnmatch主要通过fnmatch.fnmat ...

  9. winform开线程,避免页面假死

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 1.1 java学习网站

    1.http://www.rupeng.com/Courses/Index/51 2.https://www.zhihu.com/question/25255189