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

For example,
Given the following matrix:

[
[ , , ],
[ , , ],
[ , , ]
]

You should return [1,2,3,6,9,8,7,4,5].

思路:规律:当我们沿水平方向走完一次(比如从左往右),则下一次水平方向(从右向左)要走的长度为这一次减一。竖直方向也是一样的规律。

拿题目中的这个矩阵为例子,第一次水平方向走了1, 2, 3,然后竖直方向6, 9,然后又是水平方向8, 7, 然后竖直方向4, 然后水平方向5。整个过程中,水平方向走的距离依次是3,2,1,竖直方向走的距离依次是2,1。

对于一个M行N列的矩阵,则水平方向走的距离应该是N, N-1, N-2, ...., 1,竖直方向走的距离为M-1, M-2, ..., 1。

之后就简单了,我们从左上角matrix[0][0]出发,然后只要知道自己当前要走的方向和要走的距离,就能解决这个问题了。

 class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.size() == ) return res;
int height(matrix.size() - ), width(matrix[].size()), row(), col(-);
bool goRow(true), goForward(true);
while ((goRow && width > ) || (!goRow && height > )) {
if (goRow) {
for (int i = ; i < width; i++)
res.push_back(goForward ? matrix[row][++col] : matrix[row][--col]);
width--;
goRow = !goRow;
} else {
for (int i = ; i < height; i++)
res.push_back(goForward ? matrix[++row][col] : matrix[--row][col]);
height--;
goRow = !goRow;
goForward = !goForward;
}
}
return res;
}
};

Spiral Matrix -- LeetCode的更多相关文章

  1. Spiral Matrix leetcode java

    题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...

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

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

  3. [LeetCode] Spiral Matrix 螺旋矩阵

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

  4. 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 ...

  5. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  6. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  7. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  8. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  9. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

随机推荐

  1. CentOS 6.3 下 vsftp搭建

    环境:CentOS6.3 ftp的三种用户模式 匿名用户:vsftp默认开启匿名用户,但只允许下载不允许上传:匿名用户anonymous或ftp:匿名用户目录/var/ftp,但实际上vsftp对匿名 ...

  2. springmvc项目搭建三-添加前端框架

    这几年前端框架发展可以说非常迅猛了...实际项目中也用到了几个,easyui相对来讲,算是我第一个接触的前端框架了,用的时候感觉很方便,省了很多代码量,一个好的前端框架可以为你省去很多精力在前端布局上 ...

  3. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. BZOJ1483 [HNOI2009]梦幻布丁 【链表 + 启发式合并】

    题目 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色. 例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入格式 第一行给出N,M表示 ...

  5. 回文后缀(suffix)

    回文后缀(suffix) 题目描述 给定字符集大小 SS ,问有多少个长度为 NN 的字符串不存在长度 >1>1 的回文后缀. 答案对 MM 取模. 输入格式 第一行两个正整数 n, kn ...

  6. atan 和 atan2

     转自http://blog.csdn.net/chinabinlang/article/details/6802686 atan函数与atan2函数的一点区别 . atan 和 atan2 都是求反 ...

  7. 洛谷P1160 队列安排

    题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进队列,这时队列中只有他一个人: 2.2-N号同学依次入列,编号为i的同学入列方式为:老师 ...

  8. 【ZOJ4062】Plants vs. Zombies(二分)

    题意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai, 现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走m步,问最矮的 ...

  9. windows7下检测耳机麦克拔插(转)

    原文转自 https://blog.csdn.net/rankun1/article/details/50972990 #include "stdafx.h" #define SA ...

  10. python自动化测试windows gui

    http://sourceforge.net/projects/pywinauto/files/pywinauto/ http://www.microsoft.com/en-us/download/c ...