【数组】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 ...
随机推荐
- ansible-playbook 主机变量2
ansible-playbook 配置 hosts 后可以指定变量,通过-k 可以交互输入密码,也可以将密码写在 hosts 文件中. 入口 yaml 文件中通过 {{ ** }} 获取变量,命令行通 ...
- 修改Python IDLE代码配色及语法高亮主题
初学Python,想必大家拿来练习最多的IDE就是Python自带的IDLE了,但是默认的代码配色及语法高亮主题确实很不适应,所以我们需要做个小小的美化,比如像下面这样我做的美化配置: HOW TO ...
- struts2 file
JavaBean 中: private File[] pic; private String[] picContentType; private String [] picFileName; sett ...
- SBIT
SBIT chmod -R o+t dirs/ 给指定目录设置保护,只有所有者才能删除.
- (小数化分数)小数化分数2 -- HDU --1717
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1717 举例: 0.24333333…………=(243-24)/900=73/3000.9545454…… ...
- PAT甲 1002. A+B for Polynomials (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- x13 vs md5
x13 vs md5 阅读: 评论: 作者:Rybby 日期: 来源:rybby.com 最近在设计巴巴变时想对用户设计的节点模块添加锁定功能,比如你的网站可以让用户发表文章或评论,而你想让用 ...
- ACL授权实例
上一篇关于ACL的文章中:位运算实现ACL授权与认证过程的原理解析,我们学习了通过位运算实现ACL授权与认证的原理核心,今天我们一起来看授权的实例. 实现的功能很简单:打开授权界面时,加载已授权信息. ...
- PGF基本图形对象
\documentclass{article} \usepackage[active ,tightpage ,xetex ]{ preview} \usepackage{tikz} \begin{do ...
- matlab toolboxes 大全
MATLAB Toolboxes top (Top) Audio - Astronomy - BiomedicalInformatics - Chemometrics - Chaos - Chemi ...