Spiral Matrix螺旋遍历矩阵
假定有:
[
[ 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螺旋遍历矩阵的更多相关文章
- [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 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [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 ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [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 ...
- spiral matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
随机推荐
- Spring 应用外部属性文件 配置 context 错误
在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...
- Python Numpy 数组的初始化和基本操作
一.基础: Numpy的主要数据类型是ndarray,即多维数组.它有以下几个属性: ndarray.ndim:数组的维数 ndarray.shape:数组每一维的大小 ndarray.size:数组 ...
- IBM Rational AppScan使用详细说明
转自:http://www.nxadmin.com/tools/675.html 本文将详细介绍Appscan功能选项设置的细节,适合E文一般,初次接触Appscan的童鞋参考阅读. Appscan是 ...
- Linux的文件传输命令总结
由于工作原因,须要常常在不同的server见进行文件传输,特别是大文件的传输,因此对linux下不同server间传输数据命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp ...
- Struts2学习之拦截器栈
© 版权声明:本文为博主原创文章,转载请注明出处 拦截器栈: - 从结构上看:拦截器栈相当于多个拦截器的组合 - 从功能上看:拦截器栈也是拦截器 默认拦截器栈: - 在struts-core.jar中 ...
- Python基础之模块2
如何导入多个模块? import re #单行导入多个模块 '''多行导入多个模块''' import re import sys import os 如何给模块起别名? import my_modu ...
- Eclipse Plugin Installation and Windows User Access Control
I make Eclipse Plugins and I sell them to developers using Eclipse. Most of the visitors to my web s ...
- Keepalived + MySQLfailover + GTIDs 高可用
架构图 10.1.1.207 mysql master + keepalived 10.1.1.206 mysql slave ( backup master ) + ke ...
- iOS tableView Section圆角方案
给tableView的section设置圆角 首先给让cell左右偏移一点的距离,通过重写cell的setframe方法来实现 -(void)setFrame:(CGRect)frame{ CGFlo ...
- MUI 清除缓存
mui 清除但是在ios和安卓稍微有点区别, ios可以清除的很彻底,下载文件也能删除: 安卓能清理缓存,但是不能删除下载的文件: plus.cache.calculate(function(size ...