博客
关于我
剑指 Offer 29. 顺时针打印矩阵
阅读量:373 次
发布时间:2019-03-05

本文共 1890 字,大约阅读时间需要 6 分钟。

顺时针打印矩阵

问题描述

给定一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

示例

示例1:输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]输出:[1,2,3,6,9,8,7,4,5]

示例2:输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]输出:[1,2,3,4,8,12,11,10,9,5,6,7]

约束条件

  • 0 ≤ matrix.length ≤ 100
  • 0 ≤ matrix[i].length ≤ 100

方法一:普通解法

方法思路

我们可以通过逐层遍历矩阵来实现顺时针打印。具体步骤如下:

  • 初始化四个指针:left(左边界)、right(右边界)、top(上边界)、bottom(下边界)。
  • 顺序遍历:
    • 从左到右遍历当前的行。
    • 从上到下遍历当前的列。
    • 从右到左遍历当前的行。
    • 从下到上遍历当前的列。
  • 逐步收缩四个指针,直到所有元素都被访问。
  • 解决代码

    #include 
    using namespace std;vector
    spiralOrder(vector
    > matrix) { vector
    res; if (matrix.empty()) return res; int left = 0, right = matrix[0].size() - 1; int top = 0, bottom = matrix.size() - 1; while (true) { // 从左到右 for (int i = left; i <= right; ++i) { res.push_back(matrix[top][i]); } if (++top > bottom) break; // 从上到下 for (int i = top; i <= bottom; ++i) { res.push_back(matrix[i][right]); } if (left > --right) break; // 从右到左 for (int i = right; i >= left; --i) { res.push_back(matrix[bottom][i]); } if (top > --bottom) break; // 从下到上 for (int i = bottom; i >= top; --i) { res.push_back(matrix[i][left]); } if (++left > right) break; } return res;}

    代码解释

    • 初始化四个指针:leftrighttopbottom,分别表示矩阵的左、右、上、下边界。
    • 使用while循环逐层遍历矩阵。
    • 每层遍历时,分别从左到右、上到下、右到左、下到上打印元素。
    • 逐步收缩边界指针,直到所有元素都被访问。

    方法二:神仙解法

    方法思路

    这个方法利用了矩阵的转置和翻转特性:

  • 将矩阵的行转换为列,通过zip(*matrix)实现。
  • 将转置后的矩阵沿着垂直轴翻转,即[::-1]
  • 逐次将第一行移除,直到矩阵为空。
  • 这种方法非常简洁,易于实现。

    解决代码

    def spiralOrder(matrix):    res = []    while matrix:        res += matrix.pop(0)        matrix = list(zip(*matrix))[::-1]    return res

    代码解释

    • 使用while循环处理非空矩阵。
    • 每次将矩阵的第一行添加到结果列表中。
    • 将剩余的矩阵转置并翻转,继续处理。
    • 当矩阵为空时,循环结束并返回结果。

    总结

    以上是两种解决方案的实现方式。第一种方法通过逐层遍历实现,逻辑清晰但代码稍微复杂;第二种方法利用了矩阵的转置和翻转特性,实现简洁但对转置操作有一定的性能消耗。根据具体需求选择使用哪种方法都可以。

    转载地址:http://ireg.baihongyu.com/

    你可能感兴趣的文章
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No static resource favicon.ico.
    查看>>
    no such file or directory AndroidManifest.xml
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>