题目链接:https://leetcode.com/problems/spiral-matrix/description/

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

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]

思路:

  • 题目要求给定一个m * n的矩阵,按螺旋式的方式将其存入一个容器中
  • 针对此类型的题目,其实就是找规律,最好的方法还是:观察!观察!观察!重要的事说三遍!!!
  • 下面说下思路:
  • 为了便于分析,将每次扫描矩阵边的四个角分别命名为:left_Up、right_Up、right_Down、left_Down;

  • 观察可知一个完整的遍历顺序为:left_Up->right_Up->right_Down->left_Down->left_Up......,依次类推,只不过下一次遍历的范围变小了;
  • 当前要解决的问题主要有如下两个:
    1. 是如何控制遍历的次数(即需要遍历几次即可遍历完整个m*n矩阵);
    2. 每一次遍历的各个遍历方向的遍历范围如何控制。
  • 通过观察发现,遍历的次数 loop = min(m, n) / 2 + min(m, n) % 2; 因为每次遍历都会遍历完成整个矩阵的两行、两列(这里的两行、两列仅仅只是指范围),而决定循环次数loop的是矩阵行数、列数中较小的那一个。
  • 用(i, j)的形式表示矩阵上每一个元素的坐标(i:表示行数; j:表示列数);i = 0, j = 0(i, j初始化为0), m: 表示矩阵的行数,n:表示矩阵的列数 。
  • 初始化:left_Up = 0, right_Up = n - j, right_Down = m - i, left_Down = 0; 矩阵从左上角left_Up开始出发
  • left_Up -> right_Up的遍历过程中,即从坐标(i, left_Up)-> (i, right_Up)的过程;i不变,left_Up在变化, 令left_Up = j,  left_Up < right_Up即为循环终止条件;
  • right_Up -> right_Down的遍历过程中,即从坐标(i+1, left_Up) -> (right_Down, left_Up)的过程;left_Up不变,令right_Up = i + 1, right_Up < right_Down即为循环终止条件;
  • right_Down -> left_Down的遍历过程中,即从坐标(right_Up, left_Up-1) -> (right_Up, left_Down)的过程;right_up不变,令right_Down = left_Up-1, right_Dwon >= j即为循环终止条件;
  • left_Down -> left_Up的遍历过程中, 即从坐标(right_Up-1, right_Down) -> (++i, right_Down)的过程;right_Down不变,令left_Down = right_Up-1, left_Down >= i即为循环终止条件;

  注意:在循环遍历过程中也要判断元素个数,确定是否遍历完成;因为可能最后一次遍历只有四个方向的遍历(left_Up->right_Up->right_Down->left_Down->left_Up......)中的某几个方向,即一次不完整的遍历。

编码如下

 class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ivtr;
if (matrix.size() == )
{
return ivtr;
} int m = matrix.size(); // 矩阵行
int n = matrix[].size(); // 矩阵列 int i = , j = ; // (i, j)
int loop = min(m, n) / + min(m, n) % ; while (loop-- > )
{
int left_Up = , right_Up = n - j, right_Down = m - i, left_Down = ;
// 左上角到右上角遍历
for (left_Up = j; left_Up < right_Up && ivtr.size() < m * n; ++left_Up)
{
ivtr.push_back(matrix[i][left_Up]);
}
left_Up--; // 右上角到右下角遍历
for (right_Up = i + ; right_Up < right_Down && ivtr.size() < m * n; ++right_Up)
{
ivtr.push_back(matrix[right_Up][left_Up]);
}
right_Up--; // 从右下角到左下角遍历
for (right_Down = left_Up - ; right_Down >= j && ivtr.size() < m * n; --right_Down)
{
ivtr.push_back(matrix[right_Up][right_Down]);
}
right_Down++; // 从左下角到左上角遍历
++i;
for (left_Down = right_Up - ; left_Down >= i && ivtr.size() < m * n; --left_Down)
{
ivtr.push_back(matrix[left_Down][right_Down]);
}
++j; } return ivtr;
}
};

054. Spiral Matrix的更多相关文章

  1. Java for LeetCode 054 Spiral Matrix

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

  2. 054 Spiral Matrix 旋转打印矩阵

    给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...

  3. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  4. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  5. [LeetCode] Spiral Matrix 螺旋矩阵

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

  6. LeetCode - 54. Spiral Matrix

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

  7. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  8. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. 59. Spiral Matrix && Spiral Matrix II

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

随机推荐

  1. C++——INI文件详解

    原创声明:本文系博主原创文章,转载及引用请注明出处. 1. INI文件介绍 INI是英文单词 INItialization 的缩写,常作为Windows系统下的配置文件.INI文件是文本文件,通常用于 ...

  2. 如何保存ActionMailbox inbound HTML email和关于ActionText与ActiveStorage的附加

    gi代码: https://github.com/gorails-screencasts/action-mailbox-action-text/commit/3aeedc09441696c9489ed ...

  3. springboot-拦截器redis注入空问题解决

    转自:https://blog.csdn.net/liuyang1835189/article/details/81056162 主要问题是在拦截器的配置类里面,配置的类,spring容器无法获取,所 ...

  4. Linux/Centos查看进程占用内存大小的几种方法总结

    1.命令行输入top回车,然后按下大写M按照memory排序,按下大写P按照CPU排序. 2. ps -ef | grep "进程名"     ps -e -o 'pid,comm ...

  5. VMware 虚拟机下载与安装

    虚拟机下载 VMware官网地址:https://www.vmware.com/ 进行官网后,点击左边的下载图标,然后 根据操作系统选择合适的产品,在这里以Windows系统为例,点击转至下载,如下图 ...

  6. SQL 日期转换

    ), ): :57AMSELECT ), ): ), ): ), ): ), ): ), ): ), ): 06), ): ,06), ): ::46), ): :::827AMSELECT ), ) ...

  7. 【CUDA 基础】4.1 内存模型概述

    title: [CUDA 基础]4.1 内存模型概述 categories: - CUDA - Freshman tags: - CUDA内存模型 - CUDA内存层次结构 - 寄存器 - 共享内存 ...

  8. sql 语句中 order by 的用法

    order by 是用在where条件之后,用来对查询结果进行排序 order by 字段名 asc/desc asc 表示升序(默认为asc,可以省略) desc表示降序 order by 无法用于 ...

  9. AtCoder AGC007E Shik and Travel (二分、DP、启发式合并)

    题目链接 https://atcoder.jp/contests/agc007/tasks/agc007_e 题解 首先有个很朴素的想法是,二分答案\(mid\)后使用可行性DP, 设\(dp[u][ ...

  10. font属性

    font属性 font属性设置css字体