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# asp.net PhoneGap html5

    很久没写博客,今天自己写一篇吧.来谈一谈c# PhoneGap,html5 与asp.net.能搜到这篇博客就说明你是一位.net开发者,即将或者正在从事移动开发. 大家可能都有疑,我是一名.net开 ...

  2. Openjudge-NOI题库-数根

    题目描述 Description 数根可以通过把一个数的各个位上的数字加起来得到.如果得到的数是一位数,那么这个数就是数根.如果结果是两位数或者包括更多位的数字,那么再把这些数字加起来.如此进行下去, ...

  3. ORACLE 使用sqluldr2和sqlldr进行导入导出

    oracle数据导出工具sqluldr2可以将数据以csv.txt等格式导出,适用于大批量数据的导出,导出速度非常快.导出后可以使用oracle loader工具将数据导入. 简介: Sqluldr2 ...

  4. HDU 5890 Eighty seven

    预处理,$01$背包,$bitset$优化. 可以预处理出每一种询问的答案,用$01$背包计算答案,$dp[i][j][k]$表示,前$i$个数字中,选择了$j$个,能否凑出$k$这个数字,可以开成$ ...

  5. line-height属性详解

    line-height属性详解:http://www.cnblogs.com/dolphinX/p/3236686.html

  6. Gs_Class._BaseQueryWeb查询页面基类(aspx.net)

    using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...

  7. openwrt 添加 应用(luci-application)

    openwrt 添加应用的几个步骤如下: (1)在目录 ./feeds/luci/applications 下添加要增加的应用,譬如 "luci-test" (2)里面应该包含以下 ...

  8. VS2010下编译sqlite3

    首先下载源码,http://www.sqlite.org/download.html中第一个下载文件就是,下载sqlite-amalgamation-3071000.zip,当前版本是3.7.10,里 ...

  9. git出错

    查询git的版本是否装对  32 位  64位

  10. java上传并下载以及解压zip文件有时会报文件被损坏错误分析以及解决

    情景描述: 1.将本地数据备份成zip文件: 2.将备份的zip文件通过sftp上传到文件服务器: 3.将文件服务器上的zip文件下载到运行服务器: 4.将下载的zip文件解压到本地(文件大小超过50 ...