LeetCode 54. Spiral Matrix(螺旋矩阵)
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(螺旋矩阵)的更多相关文章
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [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 ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 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 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
随机推荐
- CoordinatorLayout的使用
最近项目有一个需求,做出类似闲鱼鱼塘界面的效果.如下图: 所以想到用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout去搭建此界面. Coor ...
- Android 消息机制 (Handler、Message、Looper)
综合:http://blog.csdn.net/dadoneo/article/details/7667726 与 http://android.tgbus.com/Android/androidne ...
- webservice05#soap消息
1, SOAPMessage结构图 2, SOAP消息的创建 1>前面的一个简单WebService 服务 package com.yangw.soap.service; import jav ...
- [python学习笔记] pyqt5下载与安装
下载 命令安装 pip3 install PyQt5 但是我这里老安装失败 失败问题 host='pypi.python.org', port=443): Read timed out 方案1:加大命 ...
- 指定路径下建立Access数据库并插入数据
今天刚刚开通博客,想要把我这几天完成小任务的过程,记录下来.我从事软件开发的时间不到1年,写的不足之处,还请前辈们多多指教. 上周四也就是2016-04-14号上午,部门领导交给我一个小任务,概括来讲 ...
- JSP内置对象的实验报告,页面登陆设计
JSP内置对象的实验报告 一.实验目的: 本实验的目的是让学生掌握怎样在JSP中使用内置对象request.page.response等. 二.实验要求: 编写四个JSP 页面login.jsp.Re ...
- Java 自增(++) 和 C语言中自增的区别
在Java.c语言等高级语言中自增和自减的作用基本一致,都是变量自身加一或减一.下面我只对自增进行说明,自减是类似的. 自增运算符(++),有两种书写形式,一个是在变量前: ++ num; 另一种在变 ...
- mysql操作sql的小技巧
本篇集中整理一下执行sql的小技巧,这种方式不仅带来了操作上的便捷,也可以保证数据可以数据的安全性. 1:查询数据(保证查询性能) 首先想先解释一下 SELECT * 和 SELECT t.id , ...
- linux cpu load学习笔记
linux系统中的Load对当前CPU工作量的度量 Load Average 就是一段时间 (1 分钟.5分钟.15分钟) 内平均Load. [root@CNC-BJ-5-3N1 ~]# w 20:0 ...
- vim与sublime,程序员的屠龙刀和倚天剑
对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持项目管理,界面难看-- ...