博客
关于我
给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。
阅读量:374 次
发布时间:2019-03-05

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

二维数组的顺时针螺旋遍历是一种常见的算法问题,主要用于遍历矩阵中的元素按照特定顺序排列。以下是优化后的详细解释和解决方案:

理解顺时针螺旋遍历

顺时针螺旋遍历的核心思想是逐层从矩阵的外向内遍历,每一层都按顺时针方向收缩矩阵范围。具体来说,每一层包括四个步骤:

  • 左到右遍历当前层的顶行。
  • 上到下遍历当前层的右列。
  • 右到左遍历当前层的底行。
  • 下到上遍历当前层的左列。
  • 通过逐步收缩矩阵范围(即调整top、bottom、left、right指针),可以层层递进地遍历整个矩阵。

    解决方案代码

    import java.util.ArrayList;import java.util.List;public class Solution {    public List
    spiralOrder(int[][] matrix) { List
    res = new ArrayList<>(); if (matrix.length == 0) { return res; } int top = 0; int bottom = matrix.length - 1; int left = 0; int right = matrix[0].length - 1; while (top <= bottom && left <= right) { // 左到右遍历顶行 for (int i = left; i <= right; i++) { res.add(matrix[top][i]); } top++; // 上到下遍历右列 for (int i = top; i <= bottom; i++) { res.add(matrix[i][right]); } right--; // 右到左遍历底行 if (top <= bottom) { // 确保还有行存在 for (int i = right; i >= left; i--) { res.add(matrix[bottom][i]); } bottom--; } // 下到上遍历左列 if (left <= right) { // 确保还有列存在 for (int i = bottom; i >= top; i--) { res.add(matrix[i][left]); } left++; } } return res; }}

    代码解释

  • 初始化指针topbottomleftright指针分别初始化为矩阵的顶行、底行、左列和右列。
  • 主循环:只要top未超过bottomleft未超过right,继续循环处理每一层。
  • 左到右遍历:遍历当前层的顶行,从左到右依次添加元素到结果列表中,然后将top指针向下移动。
  • 上到下遍历:遍历当前层的右列,从顶行的下一行到底行依次添加元素,然后将right指针向左移动。
  • 右到左遍历:如果还有行存在,遍历当前层的底行,从右到左依次添加元素,然后将bottom指针向上移动。
  • 下到上遍历:如果还有列存在,遍历当前层的左列,从底行向上依次添加元素,然后将left指针向右移动。
  • 测试案例

    假设输入矩阵为:

    1 2 34 5 67 8 9

    按照上述算法,输出顺序应该是:1, 2, 3, 6, 9, 8, 7, 4, 5。

    优化建议

    • 边界处理:在每一步遍历后,检查是否还有行或列存在,避免重复遍历或越界。
    • 指针调整:每完成一个方向遍历后,相应的指针向内移动,逐步收缩矩阵范围。
    • 清晰的注释:确保代码中有足够的注释,方便其他开发者理解算法逻辑。

    通过以上方法,可以高效地实现二维数组的顺时针螺旋遍历,适用于处理类似的问题。

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

    你可能感兴趣的文章
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>