Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
解题思路:
参考例二,观察索引改变方式:(0,0)--->(0,3)、(0,3)--->((2,3)--->(2,0)--->(1,0)--->(1,2)
从(0,3)看,分别是:向下 横坐标自增1,到2;向左:纵坐标自减1 ,到0;向上横坐标自减1,到1;向右纵坐标自增1,到2
假如m*n的矩阵,从(0,m-1)开始,向下移动n-1次到达最下面,再向左m-1次,向上n-2次,向右m-2次,接着就是:向下n-3,向左m-3,向上n-4,向右m-4。每次转向m或n都会自减1。
这是我的思路,网上很多都是直接操作索引坐标,我觉得不是很好理解,因为超过一个螺旋的矩阵,每次都要更改参考坐标,不过两种方法本质差别不大
java:
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List nums=new ArrayList();
if (matrix.length==0||matrix[0].length==0)return nums ;
int row=matrix.length-1,col=matrix[0].length-1,m=0,n=0,i=-1,tmp=0;
while (row>=0&&col>=0){
switch (i++%4){
case 0:
for (tmp=0;tmp<row;tmp++)
nums.add(matrix[++m][n]);row-=1;
break;
case 1:
for (tmp=0;tmp<col;tmp++)
nums.add(matrix[m][--n]);col-=1;
break;
case 2:
for (tmp=0;tmp<row;tmp++)
nums.add(matrix[--m][n]);row-=1;
break;
case 3:
for (tmp=0;tmp<col;tmp++)
nums.add(matrix[m][++n]);col-=1;
break;
default:
for (tmp=0;tmp<=col;tmp++)
nums.add(matrix[m][n++]);tmp=0;n-=1;
break;
}
}
return nums;
}
}
注意点:
先判断是否为空数组,判断条件顺序不能颠倒。因为如果 matrix.length==0 判断为true,则后面的 matrix[0].length==0 不会再判断,即返回空数组;但是matrix[0].length==0 在前时,如果输入数组为空,matrix[0] 会报错因为matrix并没有0号索引。
python3:
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if len(matrix)==0 or len(matrix[0])==0:return []
nums=[];m=0;n=0;row=len(matrix)-1;col=len(matrix[0])-1;flag=0;
for n in range(col+1):nums.append(matrix[m][n])
while row>=0 and col>=0:
if flag % 4 == 0:
for i in range(row):
m+=1
nums.append(matrix[m][n])
row -= 1
elif flag % 4==1:
for i in range(col):
n-=1
nums.append(matrix[m][n])
col -= 1
elif flag % 4 == 2:
for i in range(row):
m-=1
nums.append(matrix[m][n])
row -= 1
elif flag % 4 == 3:
for i in range(col):
n+=1
nums.append(matrix[m][n])
col -= 1
flag+=1
return nums
注意点:
python没有switch...case...语句。for循环可操作性很高,可以直接操作索引坐标改变遍历方式,不再赘述。
Leetcode 54:Spiral Matrix 螺旋矩阵的更多相关文章
- 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]54. Spiral Matrix螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 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 ...
- [LeetCode] 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 分) ...
随机推荐
- I.MX6 wpa_applicant 开启 debug 输出
/*********************************************************************** * I.MX6 wpa_applicant 开启 de ...
- 转载:百为STM32开发板教程之十一——NOR FLASH
转载:http://bbs.21ic.com/icview-586199-1-1.html 百为STM32开发板教程之十一——NOR FLASH 参考文档:百为stm32开发板光盘\st官方参考资料\ ...
- Ubuntu 环境变量及 ADB 配置 (转载)
转自:http://blog.csdn.net/ithomer/article/details/7307449 同Windows一样,Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环 ...
- SAS基础 -- SAS编程入门
SAS语言 -- 简介 SAS语言是一种专用的数据管理与分析语言,它提供了一种完善的编程语言.类似于计算机的高级语言,SAS用户只需要熟悉其命令.语句及简单的语法规则就可以做数据管理和分析处理工作 ...
- 安装配置Eclipse Python开发插件PyDev
一.PyDev安装的版本要求 PyDev是支持在Eclipse中进行Python程序开发的插件,Pydev官方的说法是需要安装 java 8 and Eclipse 4.6 (Neon),当然,你也可 ...
- 【TIDB】2、TIDB进阶
0.TIDB优势 1.和MySql相比,具备OLAP能力.省去了很多数据仓库搭建成本和学习成本.这在业务层是非常受欢迎的.可以在其他分库分表业务中,通过 syncer 同步,进行合并,然后进行统计分析 ...
- Workflow 规则大全 最新版
对于怎么操作Workflow我就不重复说明了 大家可以搜索我的另一条微博.Workflow,作为一款提高效率的软件,我觉得很有必要进行推广,当然我比较需要这里面的很多规则,先为己再为公.首先我只是出 ...
- mybatis-plus 获取新增id
<insert id="insert" parameterType="com.xxx.xxxx.pojo.User"> insert into t_ ...
- 数据结构 - 链队列的实行(C语言)
数据结构-链队列的实现 1 链队列的定义 队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已, 我们把它简称为链队列.为了操作上的方便,我们将队头指针指向链队列的头结点,而队尾指针指 ...
- [POJ2750]Potted Flower
Description The little cat takes over the management of a new park. There is a large circular statue ...