【数组】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]
.
思路:
逐个环的打印, 对于m *n的矩阵,环的个数是 Math.ceil((Math.min(m,n))/2)。对于每个环顺时针打印四条边。
注意的是:最后一个环可能只包含一行或者一列数据
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if(matrix.length==0||matrix==null){
return [];
}
var m=matrix.length,n=matrix[0].length;
var circle=Math.ceil((Math.min(m,n))/2); var a=m,b=n,res=[];
for(var i=0;i<circle;i++,a-=2,b-=2){
for(var col=i;col<i+b;col++){
res.push(matrix[i][col]);
}
for(var row=i+1;row<i+a;row++){
res.push(matrix[row][i+b-1]);
}
if(a==1||b==1)break;
for(var col=i+b-2;col>=i;col--){
res.push(matrix[i+a-1][col]);
}
for(var row=i+a-2;row>i;row--){
res.push(matrix[row][i]);
} } return res; };
【数组】Spiral Matrix的更多相关文章
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- Spiral Matrix
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- PAT1105:Spiral Matrix
1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- A1105. Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
随机推荐
- jQuery链式调用
<script> var arr = function(){ return new arr.prototype.init(); } arr.prototype.init = functio ...
- cmd运行命令
winver检查Windows版本 dxdiag检查DirectX信息 mem.exe显示内存使用情况 Sndvol32音量控制程序 sfc.exe系统文件检查器 gpedit.msc 组策略 reg ...
- svn错误:Can't convert string from 'UTF-8' to native encoding
如果文件名包含了中文,当执行"svn up ."遇到如下错误时: svn: Can't convert string from 'UTF-8' to native encoding ...
- Exception in thread Thread-3:第三个线程意外
Status:Executing -train.py 02/09/2019 09:33:38 INFO Log level set to: INFO Using TensorFlow backend. ...
- (用了map) Registration system
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93241#problem/C (654123) http://codeforces.com ...
- POJ1062不错的题——spfa倒向建图——枚举等级限制
POJ1062 虽然是中文题目但是还是有一定几率都不准题目意思的:1.所有可能降价的措施不是降价多少钱而是降至多少钱2.等级范围:是你所走的那一条路中所有人中最好最低等级差不允许超过limit限制 思 ...
- A River Runs Through It
Our birth is but a sleep and a forgetting: The Soul that rises with us, our life's Star, ...
- VSTO学习笔记
文档类型程序发布: 安装.NetFrameWork Visual Studio 2010 Tools for Office Runtime 4.0下载地址: http://www.microsoft. ...
- 设计模式之状态模式(State Pattern)
一.什么是状态模式? 把所有动作都封装在状态对象中,状态持有者将行为委托给当前状态对象 也就是说,状态持有者(比如汽车,电视,ATM机都有多个状态)并不知道动作细节,状态持有者只关心自己当前所处的状态 ...
- C#写文本文件,如何换行(添加换行符)
把文本写到文件中,如果是几段文字拼合起来输出到文件中,通常每段非结尾文字后需要添加换行符,不然几段文字都变成一段. 在 C# 中,文本换行有两种方法,一种在需要换行的文本后面添加换行符 \r\n 即可 ...