题目:

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. linux的客户端安装步骤配置

    现有一个***.tar.gz的客户端软件,现在客户端安装步骤如下 说明:此软件是一个网络客户端认证软件,因为采用虚拟机中的linux主机,所以此主机的网路配置从只能使用Bridge模式,而不能使用NA ...

  2. Android-Xml,PullParser,解析

    在上一篇博客,Android-XML格式描述,介绍来XML在Android中的格式: 解析persons.xml文件内容: <?xml version="1.0" encod ...

  3. zabbix-server启动报错解决

    启动zabbix-server有如下报错: 29171:20180714:084911.367 cannot start alert manager service: Cannot bind sock ...

  4. Eclipse ADT 与VS 常用的快捷键 对比学习

    注:以下说的类型于VS,是指:VS+Resharper的快捷键,我是采用了Resharper作为VS的快捷键. 导航 Ctrl+1 快速修复 (类似于VS的alt+enter) Ctrl+D: 删除当 ...

  5. TFS 2015 生成不输出任何结果

    这是一台用于测试的TFS 2015服务器,其中的数据是通过备份和还原,在使用应用层专用的方式配置出来的. 在配置完成代理服务器以后,运行生成,发现在获取代码之前就停止失败了,并且在生成过程中没有任何结 ...

  6. 【转】Swig Getting Started

    Installation Via NPM: $ npm install swig --save Basic Usage Swig has multiple ways to compile and re ...

  7. asp.net mvc5 分析器错误消息: 未能加载类型“XXX.MvcApplication”

    描述 今天忽然碰到一个这个错误: “/”应用程序中的服务器错误. 分析器错误 说明: 在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件. 分析器错误消息: 未能加 ...

  8. Python:Selenium+Webdriver安装

    本人小白一枚,今天在使用selenium+webdriver的时候遇到了一个小问题: WebDriverException: 'chromedriver' executable needs to be ...

  9. event 自定义事件

    自定义事件 1 public class Program 2 { 3 public event EventHandler ehdl=null; 4 public Program() 5 { 6 ehd ...

  10. session 和cookie

    (1)cookie与session---------->>>>>>>>>>>>>>>>>>& ...