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 ...
随机推荐
- WDCP管理面板忘记ROOT MYSQL密码及重置WDCP后台登录密码方法
不管出于何种原因,应该有不少的朋友在自己的VPS/服务器上采用WDCP管理面板的时候有忘记MYSQL ROOT账户管理密码在寻找解决方法,甚至有忘记WDCP后台管理登录密码的.这些问题都比较简单,只需 ...
- Delphi里J+开关作用类似C语言的static变量
從前筆者曾經對以下的程式產生過疑惑:{$J+}procedure TForm1.Button1Click(Sender: TObject);const VarConst: integer = 4;b ...
- list集合转换成json类型
public String gettext(HttpServletRequest request,HttpServletResponse response){ List<xuanhuan_> ...
- 二分Kmeans的java实现
刚刚研究了Kmeans.Kmeans是一种十分简单的聚类算法.可是他十分依赖于用户最初给定的k值.它无法发现随意形状和大小的簇.最适合于发现球状簇.他的时间复杂度为O(tkn).kmeans算法有两个 ...
- java多线程编码注意事项
Sole purpose of using concurrency is to produce scalable and faster program. But always remember, sp ...
- 学习IIS & MVC的运行原理
我一直疑惑于以下问题,从客户端发出一个请求,请求到达服务器端是怎样跟iis衔接起来的,而iis又是怎样读取我发布的代码的,并返回服务器上的文件.这其中是怎样的一个处理过程. 1:当你从浏览器中输入一个 ...
- Swing实现右下角消息框
package com.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; impo ...
- HDFS源码分析数据块复制监控线程ReplicationMonitor(二)
HDFS源码分析数据块复制监控线程ReplicationMonitor(二)
- xml schema复杂类型
xml schema复杂类型 对于复杂类型,xs:complexType, xs:sequence子节点必须有. <?xml version="1.0"?> <x ...
- php-fpm重启失败报错
php-fpm启动命令:/usr/local/php5/sbin/php-fpm 报错:ERROR: unable to bind listening socket for address '127. ...