题目链接: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. css改变背景透明度

    透明往往能产生不错的网页视觉效果,先奉上兼容主流浏览器的CSS透明代码:.transparent{filter:alpha(opacity=90); -moz-opacity:0.9; -khtml- ...

  2. php将数组中某个元素置顶设为第一个元素

    一个数组$a0有N个元素,要将其中第3个元素,排在数组的首位. 第一种做法是: 取出第3个元素,赋值给变量$a unset 第3个元素 array_unshift 将$a添加到数组头部. 如果是数字下 ...

  3. GIT和SVN的区别(面试)

    Cit是分布式,而SVN不是分布式 存储内容的时候,Git按元数据方式存储,而SVN是按文件 Git没有一个全局版本号,SVN有,目前为止这是SVN相比Git缺少的最大的一个特征 Git的内容完整性要 ...

  4. --print-defaults打印mysqld启动加载配置

    Mysql启动配置文件加载路径     Mysql可以读取到的配置文件         /etc/my.cnf         /etc/mysql/my.cnf         /usr/local ...

  5. 解决Android无法正常https://dl.google.com/dl/android/maven2/com/的办法

    最近需要进行移动开发,在安装Android Studio时,遇到了很纠结的问题,安装一直很不顺利.经过2天的百度搜索终于是找到解决的办法. 问题花了2天的时间才从茫茫大海中找到确切的答案.所以必须开个 ...

  6. kylin实战(一)

    kylin适用场景 OLAP 它适合数据量大,查询维度多,但是业务改动不频繁的场景.因为业务多,则kylin的cube很多.每次业务变更,kylin修改的工作量大,且每次全量跑数据耗费时间比较长. 它 ...

  7. 题解 [ZJOI2008]树的统计Count

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u ...

  8. CodeForces 788A - Functions again [ DP ]

    反着求一遍最大连续子序列(前项依赖) #include <bits/stdc++.h> using namespace std; #define LL long long ; int n; ...

  9. Confluence 6.15 博客页面(Blog Posts)宏参数

    参数是让你可以用来控制宏的格式和输出的选项.在 Confluence 存储格式或者 Wiki 标记(wikimarkup)中使用的参数名与在宏浏览器中使用的标签名是不同的,在下面我们将会用括号列出  ...

  10. URAL 2092 Bolero 贪心

    C - Bolero Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...