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. 基于Python的selenuim自动化测试尝试

    工作这么多年了,终于狠下心好好开始学学自动化测试相关知识,揭开这层神秘的面纱. 困难重重,障碍很多,但好在每天都多少有点小收获. 很感谢一个QQ好友推荐的虫师,也非常感谢在这个契机读到了虫师编著的&l ...

  2. jeakins忘记密码时的处理(简单粗暴)

    1.打开config文件(通过ps -elf | grep jenkins查看JENKINS_HOME目录,然后在目录下查找config.xml文件) 2.修改<useSecurity>t ...

  3. python 学习分享-实战篇类 Fabric 主机管理程序开发

    # 类 Fabric 主机管理程序开发: # 1. 运行程序列出主机组或者主机列表 # 2. 选择指定主机或主机组 # 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) # 4. 充分 ...

  4. Vue_初识

    前端三大框架: vue:开发效率相当高了. angalar:适合做后台管理系统,入手容易,但是越往后会越难受. react:虚拟dom(渲染内存中存储的dom,经过操作后,才会去渲染浏览器的真实dom ...

  5. 孤荷凌寒自学python第十五天python循环控制语句

    孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...

  6. mac系统安装/升级node

    一.安装 1.node 是通过brew来安装的,所以第一步先安装brew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Ho ...

  7. [洛谷P4925][1007]Scarlet的字符串不可能这么可爱

    题目大意:问字符集大小为$k$,长度为$L$的字符串,且没有长度超过$1$的回文段的个数.规定第$s(若为0则无限制)$位为$w$. 题解:懒得写了,根据是否有限制分类讨论 卡点:中途有个地方忘记取模 ...

  8. 一文看懂Kafka消息格式的演变

    摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在不断的升级改进,从0.8.x版本开始到现在的1.1.x版本,Kaf ...

  9. 转:android service总结2

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  10. vue实现tab切换

    需要弄类似tab切换的功能就是一个点击切换上一页下一页的页面 找到这个获得灵感 <!DOCTYPE html> <html lang="en"> <h ...