题目:

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的更多相关文章

  1. [LeetCode] Spiral Matrix 螺旋矩阵

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

  2. LeetCode - 54. Spiral Matrix

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

  3. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. Spiral Matrix

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

  5. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  6. PAT1105:Spiral Matrix

    1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...

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

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

  8. A1105. Spiral Matrix

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  9. 1105 Spiral Matrix

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  10. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

随机推荐

  1. LA 4329 Ping pong (树状数组)

    题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...

  2. Spring MVC controller 被执行两次

    interceptor 被执行两次 后来发现 时controller被执行两次 后来发现是jsp页面有个: <img src="#" > 导致被执行两次. 解决方案:去 ...

  3. Codeforces805 C. Find Amir 2017-05-05 08:41 140人阅读 评论(0) 收藏

    C. Find Amir time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. hdu1158 Employment Planning 2016-09-11 15:14 33人阅读 评论(0) 收藏

    Employment Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. Jenkins中执行docker命令报错

    Cannot connect to the Docker daemon. Is the docker daemon running on this host?   在配置Jenkins从Gitlab自 ...

  6. C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB

    C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB 我用的数据库是Oracle,就目前来看,许多数据库现在都倾向于Oracle数据库,对ORACLE数据库基本的操作也是必须 ...

  7. dorado-SplitSpanel控件

    1.这是一个界面布局控件 2.分为SideControl边区域和MainControl主区域 3.常用属性 3.1 collapsed:打开页面时,边区域是否显示 3.2 position:边区域占总 ...

  8. 遍历 SortedList<string, string> 中的值(可用于datatable转json)

    SortedList<string, string> STK = new SortedList<string, string>();STK.Add("1", ...

  9. NGUI图集字体

    UIFont里使用Symbols来指定字体时用Sprite前缀和名字自动分配的工具,前段时间工作需要时写的,具体用法有空时再写. using UnityEngine; using UnityEdito ...

  10. WPF MVVM 网易云音乐

    前言: 写的不多. 随时会更新吧 涉及到的技术有很多 绑定 集合 命令 MVVM 自定义控件 事件 用户控件 附加属性 依赖属性 动画 Style Tmepate 装饰器 转换器 ********** ...