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. Alpha 冲刺

    队名:我头发呢队 组长博客 作业博客 杰(组长) 过去两天完成了哪些任务 查阅Python爬取音源的资料,如 Python3爬虫抓取网易云音乐热评实战 Python爬取高品质QQ音乐(2) 如何爬网易 ...

  2. python与MySQL数据库

    python与MySQL数据库 慕课网连接 我使用的软件:python2.7 + MySQL+ Navicat for MySQL + Atom 注意:你的数据库表格类型的引擎为:InnoDB :字符 ...

  3. nyoj 题目44 子串和

    子串和 时间限制:5000 ms  |  内存限制:65535 KB 难度:3   描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...

  4. nodejs & docker

    nodejs & docker https://github.com/xgqfrms-GitHub/Node.js/blob/master/Docker-Nodejs/translation. ...

  5. TensorFlow应用实战 | TensorFlow基础知识

    挺长的~超出估计值了~预计阅读时间20分钟. 从helloworld开始 mkdir 1.helloworld cd 1.helloworldvim helloworld.py 代码: # -*- c ...

  6. 《R语言实战》读书笔记--为什么要学

    本人最近在某咨询公司实习,涉及到了一些数据分析的工作,用的是R语言来处理数据.但是在应用的过程中,发现用R很不熟练,所以再打算学一遍R.曾经花一个月的时间看过一遍<R语言编程艺术>,还用R ...

  7. Codeforces Round #357 (Div. 2) B

    B. Economy Game time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  8. Disable or enable the IPv6 protocol in Red Hat Enterprise Linux

    Resolution Red Hat Enterprise Linux 4, 5 and 6 enable Internet Protocol Version 6 (IPv6) by default. ...

  9. mysql索引记

    如果字段是数值型,where 是字符串型,走索引但是,如果字段是字符串型,但是where 是数值型,不走索引

  10. Android键盘面板冲突 布局闪动处理方案

    转:来自Android键盘面板冲突 布局闪动处理方案 起源,之前在微信工作的时候,为了给用户带来更好的基础体验,做了很多尝试,踩了很多输入法的坑,特别是动态调整键盘高度,二级页面是透明背景,魅族早期的 ...