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. MQ消息中间件

    MQ是什么? MQ是Message Queue消息队列的缩写.消息队列是一种应用程序对应用程序的通信方法.应用程序通过写和检索入列队的针对应用程序的数据(消息)来进行通信,而不需要专用连接来链接它们. ...

  2. 每天一个Linux命令(7):pwd命令

    pwd命令以绝对路径的方式显示用户当前工作目录. 语法 pwd(选项) 选项 --help:显示帮助信息: --version:显示版本信息. 实例 [root@localhost ~]# pwd / ...

  3. selenium定位弹出菜单

    写selenium脚本,在浏览器定位各种弹出菜单时,有时用工具很难去取菜单的属性,下面说下如何去取: 点开firebug ,切换到“脚本”界面,首先在输入框输入单字母s,待弹出下拉列表后,单击左侧的插 ...

  4. 《Android权威编程指南(The Big Nerd Ranch Guide)(第二版)》12.4挑战练习

    本书第12章是讲解Dialog.12.4挑战练习是在CriminalIntent项目中,再增加一个TimePickerFragment的对话框fragment.通过在CrimeFragment用户界面 ...

  5. day04_08-while查询所有行

    <?php $link = @mysql_connect('localhost','root',''); mysql_query('use test',$link); mysql_query(' ...

  6. 遍历列表,打印:我叫name,今年age岁,家住dizhi,电话phone(我是通过下标取键得到对应值,有哪位大神来个更简单的)

    lt = [ {'name':'小王', 'age':18, 'info':[('phone', '123'), ('dizhi', '广州')]}, {'name':'小芳', 'age':19, ...

  7. restorator 运行后其他所有EXE文件都无法运行的解决方案

    昨天要反编译一个EXE,用RESTORATOR来查看资源罗列情况,倒霉的事情发生了,所有EXE文件点右键后‘打开’都没有了,刚开始以为中度了,进安全模式看,发现文件都没有异常,并且在安全模式下问题照样 ...

  8. Python函数参数中的冒号与箭头

    在一些Python的工程项目中,我们会看到函数参数中会有冒号,有的函数后面会跟着一个箭头,你可能会疑惑,这些都是什么东西? 其实函数参数中的冒号是参数的类型建议符,告诉程序员希望传入的实参的类型.函数 ...

  9. [bzoj] 1878 HH的项链 || 莫队

    原题 给定长为 n 的一个序列,接下来 m 次询问,每次询问区间 [ l , r ] 内有多少个不同的数. 莫队: 离线\(O(n\log(n))\). 将序列分块. 以左端点所在块为第一关键字,右端 ...

  10. HTML,CSS,font-family:中文字体的英文名称【转载】

    转自:http://www.9958.pw/post/html_font-family 宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft ...