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 ...
随机推荐
- Android提供支持不同屏幕大小的各种方法
1 http://blog.csdn.net/guolin_blog/article/details/8830286 (手机平板,通过large-layout来区分两条布局文件) 2 http:// ...
- Memcached的LRU和缓存命中率
缓存命中率 命中:直接从缓存中读取到想要的数据. 未中:缓存中没有想要的数据,还需要到数据库进行一次查询才能读取到想要的数据. 命中率越高,数据库查询的次数就越少. 读取缓存的速度远比数据库查询的速度 ...
- Android採用async框架实现文件上传
页面效果 须要的权限 <uses-permission android:name="android.permission.INTERNET"/> 网络訪问权限; 布局文 ...
- 实现Nullable 可空类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace demo ...
- Ubuntu 16.04 关闭/打开笔记本触摸板
由于笔记本触摸板太多灵敏,影响使用,所以禁用掉触摸板. 禁用触摸板命令: sudo rmmod psmouse 启用触摸板命令 sudo modprobe psmouse 注意:启用之后可能会有几秒钟 ...
- js读取json包装的map集合
后台 Map<String,Integer> map = new HashMap<>(); map.put("你好1", 1); map.put(" ...
- CG标准函数库——数学函数(GPU编程与CG语言之阳春白雪下里巴人)
- Kerberos Ticket管理
Kerberos Ticket管理 本章介绍如何管理您的Kerberos Ticket,这里的Ticket是指Ticket-Granting-Ticket(TGT),是您访问集群中服务的凭证.我们假设 ...
- java并发编程基础---Sky
1.线程及启动和终止 1.1 线程 -进程/优先级 操作系统调度的最小单元是线程,线程是轻量级进程. 线程优先级由setPriority(int)方法来设置,默认优先级是5,等级1~10.等级越高分的 ...
- 记录-项目java项目框架搭建的一些问题(maven+spring+springmvc+mybatis)
伴随着项目框架的落成后,本以为启动就能成功的,but.... 项目启动开始报错误1:java.lang.ClassNotFoundException: org.springframework.web. ...