假定有:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]

这样一个数组矩阵,现要求对其进行顺时针方向螺旋形从外至内地遍历,即输出: [1,2,3,6,9,8,7,4,5]

分析:

将每一次360度遍历视为一个周期,每一个周期分为上右下左四个阶段,逐个进行遍历:

以下是本人的草稿,请忽略苍劲如龙卷风摧朽拉枯般的字体。。。

理清思绪后就剩下笔了:

var spiralOrder = function(matrix) {
var x1= 0, y1= 0, x2= matrix[0].length- 1, y2= matrix.length- 1;
var result= []
while(x1<=x2 && y1<=y2){
for(var y= y1; y<= y2; y ++){
result.push(matrix[x1][y])
}
for(var x= x1+ 1; x<= x2; x ++){
result.push(matrix[x][y2])
}
for(var y= y2-1; y>= y1; y --){
result.push(matrix[x2][y])
}
for(var x= x2-1; x>= x1+1; x --){
result.push(matrix[x][y1])
}
++x1
++y1
--x2
--y2
}
console.log(result)
return result
};
spiralOrder([
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
])

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 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  3. [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 ...

  4. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

  5. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

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

  6. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

    Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...

  7. spiral matrix 螺旋矩阵

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

  8. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  9. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

随机推荐

  1. Android下Slidingmenu和actionbarsherlock的使用

    1 http://blog.csdn.net/wangjinyu501/article/details/9331749  博客很多,推荐此教程,slidingmenu的demo可以演示 2 http: ...

  2. LINQ中Aggregate的用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. VB中的排序问题 15个

    首次接触VB,以下就先进行VB中的排序问题                   Dim a As Integer Dim b As Integer Dim c As Integer Dim d As ...

  4. codeforces #364c They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. 分析Memcached客户端如何把缓存数据分布到多个服务器上

    Memcached客户端可以设多个memcached服务器,它是如何把数据分发到各个服务器上,而使各个服务器负载平衡的呢? 可以看看.net版中的客户端中的源码,就可以知道 先看代码:   1 /// ...

  6. CentOS: Make Command not Found and linux xinetd 服务不能启动

    在centos 安装编译器 yum -y install gcc automake autoconf libtool make linux xinetd 服务不能启动: [root@capaa xin ...

  7. HDFS源码分析数据块复制选取复制源节点

    数据块的复制当然需要一个源数据节点,从其上拷贝数据块至目标数据节点.那么数据块复制是如何选取复制源节点的呢?本文我们将针对这一问题进行研究. 在BlockManager中,chooseSourceDa ...

  8. oracle导出sql

    1.点击要导出的表2.右键点击exportData3.选择要导出的sql语句

  9. 【Mac + Mysql + Navicat Premium】之Navicat Premium如何连接Mysql数据库

    参考文章: <mac用brew安装mysql,设置初始密码> 因为我需要连接数据库工具,需要密码,所以下面介绍如何设置.修改密码实现Navicat Premium连接Mysql数据库 建议 ...

  10. 功能强大的图片截取修剪神器:Android SimpleCropView及其实例代码重用简析(转)

    功能强大的图片截取修剪神器:Android SimpleCropView及其实例代码重用简析 SimpleCropView是github上第一个第三方开源的图片修剪截取利器,功能强大,设计良好.我个人 ...