Leetcode 54:Spiral Matrix 螺旋矩阵

栏目: 编程工具 · 发布时间: 6年前

内容简介:Given a matrix of给定一个包含​ 参考例二,观察索引改变方式:(0,0)--->(0,3)、(0,3)--->((2,3)--->(2,0)--->(1,0)--->(1,2)

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 螺旋矩阵


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python 3 the Hard Way

Learn Python 3 the Hard Way

Zed A. Shaw / Addison / 2017-7-7 / USD 30.74

You Will Learn Python 3! Zed Shaw has perfected the world’s best system for learning Python 3. Follow it and you will succeed—just like the millions of beginners Zed has taught to date! You bring t......一起来看看 《Learn Python 3 the Hard Way》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试