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. 用SecureCRT连接虚拟机

    1.Root用户进入虚拟机系统 2.打开控制台 3.永久关闭防火墙,打开sshd,这样SecureCRT才能连接 chkconfig iptables off;service sshd start 4 ...

  2. Android中AlertDialog对话框禁止按[返回键]或[搜索键]

    alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(Di ...

  3. PHP检测文件能否下载

    用php代码检测一个文件是否可以下载,网上没有找到合适的代码,自己实现了一个还挺好用的,分享给有需要的朋友. 基本原理:使用http的HEAD方法,检测报文的头里httpcode是否为200. pub ...

  4. 爬虫代码实现五:解析所有分页url并优化解析实现类

    如图,我们进入优酷首页,可以看到电视剧列表,我们称这个页面为电视剧列表页,而点击进入某个电视剧,则称为电视剧详情页.那么如何获取所有分页以及对应的详情页呢,通过下面的分页得到. 因此,首先,我们将St ...

  5. 关于数据汇总方面返回Json数据的小小心得

    在一开始的开发中,计算好相关数据,然后通过 1.拼串 2.实例化Dictory对象再通过JavaScriptSerializer转换为json. 其中,2只适合于二维数据.1适合多维数据,但拼串比较费 ...

  6. Python学习笔记——基础篇【第五周】——random & time & datetime模块

    random模块 随机数 mport random print random.random() print random.randint(1,2) print random.randrange(1,1 ...

  7. [Q]系统环境改变导致“未注册”的解决方法

    据用户反映设置账户开机密码后显示未注册, 具体表现: 1. 重装试用版,重新获取注册申请码,发现注册申请码跟原来没有发生变化. 2. 重新使用原来的授权文件注册,但打开后显示未注册. 3. 发现“** ...

  8. canvas烟花-娱乐

    网上看到一个释放烟花的canvas案例,很好看哦. 新建文本,把下面代码复制进去,后缀名改为html,用浏览器打开即可. 看懂注释后,可以自己修改烟花的各个效果.我试过让烟花炸成了心型.:-) < ...

  9. 如果有两个list<Object>只取出两个中不重复的(还可以优化,这里计数器没做好,暂时使用第三变量)

    import java.util.*; class test2{ public static void main(String[] args){ List<Integer> objList ...

  10. android设备中USB转串口demo 下载

    http://files.cnblogs.com/guobaPlayer/testUSB2Serial.apk USB转串口demo程序, 无需驱动,只要手机USB是OTG类型,插上我们的模块即可使用 ...