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 螺旋矩阵的更多相关文章

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

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

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

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

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

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. 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 ...

  6. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

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

  8. [LeetCode] Spiral Matrix 螺旋矩阵

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

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

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

随机推荐

  1. I.MX6 ifconfig: SIOCSIFHWADDR: Cannot assign requested address

    /************************************************************************** * I.MX6 ifconfig: SIOCSI ...

  2. BZOJ_2821_作诗(Poetize)_分块

    BZOJ_2821_作诗(Poetize)_分块 Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要 ...

  3. C++实现用两个栈实现队列

    /* * 用两个栈实现队列.cpp * * Created on: 2018年4月7日 * Author: soyo */ #include<iostream> #include<s ...

  4. WebLogic之eclipse安装WebLogic插件

    转自:https://blog.csdn.net/magi1201/article/details/38323775

  5. Sql Server 查询重复记录

    参考网址:http://database.51cto.com/art/201103/250046.htm SQL Server数据库多种方式查找重复记录 select * from dbo.T0058 ...

  6. bzoj P2045 方格取数加强版【最大费用最大流】

    今天脑子不太清醒,把数据范围看小了一直TTTTLE-- 最大费用最大流,每个格子拆成两个(x,y),(x,y)',(x,y)向(x,y)'连一条费用a[x][y]流量1的边表示选的一次,再连一条费用0 ...

  7. IE兼容之中文汉字请求乱码-network

    IE兼容之中文汉字请求乱码 IE控制台报错: IE网络报错: 解决方法: encodeURI('--- @ -- 子卿 -'); // "---%20@%20--%20%E5%AD%90%E ...

  8. python爬虫BeautifulSoup库class_

    因为class是python的关键字,所以在写过滤的时候,应该是这样写: r = requests.get(web_url, headers=headers) # 向目标url地址发送get请求,返回 ...

  9. [Usaco2012 Open]Balanced Cow Subsets

    Description Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk ...

  10. 2017 JUST Programming Contest 3.0 B. Linear Algebra Test

    B. Linear Algebra Test time limit per test 3.0 s memory limit per test 256 MB input standard input o ...