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


题目标签:Array

  这道题目给了我们一个矩阵,m*n,让我们返回一个螺旋矩阵排列的array,这题目名字听着挺酷炫呀。我们来看原题例子:

  1  2  3

    5

  7  8

  一共有4步骤:我们还需要设置rowBegin = 0, rowEnd = 2, colBegin = 0, colEnd = 2.

  1. 红色:从rowBegin这一排向右遍历,加入每一个matrix[rowBegin][j],   j: colBegin ~ colEnd. 遍历完要把rowBegin++, 从下一排继续;

  2. 绿色:从colEnd这一列开始向下遍历,加入每一个matrix[j][colEnd], j: rowBegin ~ rowEnd. 遍历完要把colEnd--, 从左边那列继续;

  3. 蓝色:从rowEnd这一排向左遍历,加入每一个matrix[rowEnd][j], j: colEnd ~ colBegin. 遍历完要把rowEnd--, 从上一排继续。

  4. 灰色:从colBegin这一列开始向上遍历,加入每一个matrix[j][colBegin], j: rowEnd ~ rowBegin, 遍历完要把colBegin++, 从右边那列继续。

  那什么时候要结束呢,可以利用m*n 得到一共的数量,每次加入一个,就把这个数量-1, 当数量等于0的时候意味着我们加完了所有的数字,return res 就可以了。

Java Solution:

Runtime beats 20.42%

完成日期:07/18/2017

关键词:Array

关键点:螺旋矩阵的固定模式

 public class Solution
{
public List<Integer> spiralOrder(int[][] matrix)
{
List<Integer> res = new ArrayList<Integer>(); if(matrix.length == 0)
return res; int totalNum = matrix.length * matrix[0].length; int rowBegin = 0;
int rowEnd = matrix.length - 1;
int colBegin = 0;
int colEnd = matrix[0].length - 1; while(totalNum > 0)
{
// traverse right
for(int j=colBegin; j<=colEnd && totalNum>0; j++)
{
res.add(matrix[rowBegin][j]);
totalNum--;
}
rowBegin++; // traverse Down
for(int j=rowBegin; j<=rowEnd && totalNum>0; j++)
{
res.add(matrix[j][colEnd]);
totalNum--;
}
colEnd--; // traverse left
for(int j=colEnd; j>=colBegin && totalNum>0; j--)
{
res.add(matrix[rowEnd][j]);
totalNum--;
}
rowEnd--; // traverse up
for(int j=rowEnd; j>= rowBegin && totalNum>0; j--)
{
res.add(matrix[j][colBegin]);
totalNum--;
}
colBegin++;
} return res; }
}

参考资料:

https://leetcode.com/problems/spiral-matrix/#/discuss

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 54. Spiral Matrix(螺旋矩阵)的更多相关文章

  1. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  2. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  3. [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 ...

  4. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  5. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

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

  7. LeetCode - 54. Spiral Matrix

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

  8. [LeetCode] Spiral Matrix 螺旋矩阵

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

  9. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

随机推荐

  1. Shiro第三篇【授权、自定义reaml授权】

    Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro的授权 Shiro授权的流程和认证的流程其实是差不多的: Shiro支持的授权方式 Shiro支持的授权方式有三 ...

  2. 《MySQL必知必会》[02] 多表联合查询

    1.基本连接 不同类型的数据,存储在多个表中,而所谓多表连接,就是将多个表联合返回一组输出. 1.1 等值连接 基本的连接方式非常简单,只需要在WHERE子句中规定如何关联即可,如下: SELECT ...

  3. Hibernate table schema 的设置与应用

    hibernate在实现实体映射时,DB无需强行指定.部署时会较对DB户名和密码,根据用户名以访问的表完成实体映射.如果一个帐号可以访问一个数据库的下多个表,以oracle为例用户user1下面有表t ...

  4. Spring-hibernate-BaseDao

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  5. mapreduce新旧api对比

    对比:hadoop版本1.x 新版,hadoop版本0.x 旧版 1.新api引用包一般是mapreduce ,旧版api引用的包一般是mapred 2.新api使用Job,旧版api使用JobCon ...

  6. Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

    生成Image映像文件,记录好Image的URL(下面URL为测试URL,具体请参考实际):ImageURL:https://hlmrgstoragen.blob.core.chinacloudapi ...

  7. python 脚本开发实战-当当亚马逊图书采集器转淘宝数据包

    开发环境python2.7.9 os:win-xp exe打包工具pyinstaller 界面tkinter ============================================= ...

  8. iOS 多人共享开发证书

    当多人开发时,如果已经申请了几个开发者证书和发布者证书,苹果就不允许再创建了,页面添加的地方被灰化了,所以不可能每个人都建一个开发证书,这时候需要共用一个证书了.(其实一般在我们的证书界面中应该只有一 ...

  9. java初学者(新手)应该如何选择学习教材与网站

    作者:天天向上 1.学习教材选择推荐<JAVA核心技术>,想多看点代码多练习可以找<java开发实战经典>&amp;amp;lt;img src="https ...

  10. POJ-2417-Discrete Logging(BSGS)

    Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N &l ...